Microchip® Advanced Software Framework

Quick Start Guide for USB Device Generic Module (UDI Generic)

This is the quick start guide for the USB Device Generic Module (UDI Generic) 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 HID generic (Single Interface Device)" module is used. The "USB HID generic (Composite Device)" module usage is described in Advanced Use Cases.

Setup Steps

As a USB device, it follows common USB device setup steps. Refer to USB Device Basic Setup.

Usage Steps

Example Code

Content of conf_usb.h:

#define UDI_HID_generic_ENABLE_EXT() my_callback_generic_enable()
extern bool my_callback_generic_enable(void);
#define UDI_HID_generic_DISABLE_EXT() my_callback_generic_disable()
extern void my_callback_generic_disable(void);
#include "udi_hid_generic_conf.h" // At the end of conf_usb.h file

Add to application C-file:

#define UDI_HID_GENERIC_ENABLE_EXT() my_callback_generic_enable()
extern bool my_callback_generic_enable(void);
#define UDI_HID_GENERIC_DISABLE_EXT() my_callback_generic_disable()
extern void my_callback_generic_disable(void);
#define UDI_HID_GENERIC_REPORT_OUT(ptr) my_callback_generic_report_out(ptr)
extern void my_callback_generic_report_out(uint8_t *report);
#define UDI_HID_GENERIC_SET_FEATURE(f) my_callback_generic_set_feature(f)
extern void my_callback_generic_set_feature(uint8_t *report_feature);
#define UDI_HID_REPORT_IN_SIZE 64
#define UDI_HID_REPORT_OUT_SIZE 64
#define UDI_HID_REPORT_FEATURE_SIZE 4
#define UDI_HID_GENERIC_EP_SIZE 64
#include "udi_hid_generic_conf.h" // At the end of conf_usb.h file

Add to application C-file:

static bool my_flag_autorize_generic_events = false;
bool my_callback_generic_enable(void)
{
my_flag_autorize_generic_events = true;
return true;
}
void my_callback_generic_disable(void)
{
my_flag_autorize_generic_events = false;
}
void my_button_press_event(void)
{
if (!my_flag_autorize_generic_events) {
return;
}
uint8_t report[] = {0x00,0x01,0x02...};
}
void my_callback_generic_report_out(uint8_t *report)
{
if ((report[0] == MY_VALUE_0)
(report[1] == MY_VALUE_1)) {
// The report is correct
}
}
void my_callback_generic_set_feature(uint8_t *report_feature)
{
if ((report_feature[0] == MY_VALUE_0)
(report_feature[1] == MY_VALUE_1)) {
// The report feature is correct
}
}

Workflow

  1. Ensure that conf_usb.h is available and contains the following configuration which is the USB device generic configuration:
    #define UDI_HID_GENERIC_ENABLE_EXT() my_callback_generic_enable()
    extern bool my_callback_generic_enable(void);
    Note
    After the device enumeration (detecting and identifying USB devices), the USB host starts the device configuration. When the USB generic interface from the device is accepted by the host, the USB host enables this interface and the UDI_HID_GENERIC_ENABLE_EXT() callback function is called and return true. Thus, it is recommended to enable sensors used by the generic in this function.
    #define UDI_HID_GENERIC_DISABLE_EXT() my_callback_generic_disable()
    extern void my_callback_generic_disable(void);
    Note
    When the USB device is unplugged or is reset by the USB host, the USB interface is disabled and the UDI_HID_GENERIC_DISABLE_EXT() callback function is called. Thus, it is recommended to disable sensors used by the HID generic interface in this function.
    #define UDI_HID_GENERIC_REPORT_OUT(ptr) my_callback_generic_report_out(ptr)
    extern void my_callback_generic_report_out(uint8_t *report);
    Note
    Callback used to receive the OUT report.
    #define UDI_HID_GENERIC_SET_FEATURE(f) my_callback_generic_set_feature(f)
    extern void my_callback_generic_set_feature(uint8_t *report_feature);
    Note
    Callback used to receive the SET FEATURE report.
    #define UDI_HID_REPORT_IN_SIZE 64
    #define UDI_HID_REPORT_OUT_SIZE 64
    #define UDI_HID_REPORT_FEATURE_SIZE 4
    Note
    The report size are defined by the final application.
    #define UDI_HID_GENERIC_EP_SIZE 64
    Note
    The interrupt endpoint size is defined by the final application.
  2. Send a IN report:
    uint8_t report[] = {0x00,0x01,0x02...};

Advanced Use Cases

For multiple interface use of UDI HID module, see the following:

For more advanced use of the UDI HID generic module, see the following: