Jump to main content

##

# 4.1 Common Interface

The USB Driver Common Interface definition specifies the functions and their behavior that a USB Driver must implement so that the driver can be used by the MPLAB Harmony USB Host and Device Stack.
Note: The MPLAB Harmony USB Driver for PIC32MX and PIC32MZ devices implements the USB Driver Common Interface.

The USB Driver Common Interface contains functions that are grouped as follows:

- Driver System Functions - These functions are called by MPLAB Harmony to initialize and maintain the operational state of the USB Driver. The system functions can vary between different PIC32 device USB Drivers. As such, the USB Driver Common Interface does not require these functions to be of the same type. These functions are not called by the USB Host or Device Stack and therefore are allowed to (and can) vary across different PIC32 device USB Drivers. A description of these functions, along with a description of how to initialize the USB Driver for Host, Device or Dual Role operation, is provided in the specific PIC32 device USB Driver help section (see PIC32MX USB Driver and PIC32MZ USB Driver).
- Driver General Client Functions -These functions are called by the USB Host or Device Stack to gain access to the driver.
- Driver Host Mode Client Functions - These functions are called exclusively by the USB Host Stack to operate and access the USB as a Host.
- Driver Device Mode Client Functions - These functions are called exclusively by the USB Device Stack to operate and access the USB as a Device.

The USB Driver Common Interface is defined in the `<install-dir>`\`framework\driver\usb\drv_usb.h` file. This file contains the data types and structures that define the interface. Specifically, the DRV_USB_HOST_INTERFACE structure, contained in this file, is the common interface for USB Driver Host mode functions. It is a structure of function pointers, pointing to functions that define the Driver Host mode Client functions. The following code example shows this structure and the function pointer it contains.

    // *****************************************************************************
    /* USB Driver Client Functions Interface (For Host mode)

      Summary:
        Group of function pointers to the USB Driver Host mode Client Functions.

      Description:
        This structure is a group of function pointers pointing to the USB Driver
        Host mode Client routines. The USB Driver should export this group of
        functions so that the Host layer can access the driver functionality.

      Remarks:
        None.
    */typedefstruct
    {
        /* This is a pointer to the driver Open function. This function may be
         * called twice in a Dual Role application, once by the Host Stack and then
         * by the Device Stack */
        DRV_HANDLE (*open)(const SYS_MODULE_INDEX drvIndex, const DRV_IO_INTENT intent);

        /* This is pointer to the driver Close function */
        void (*close)(DRV_HANDLE handle);

        /* This is a pointer to the event call back set function */
        void (*eventHandlerSet)(DRV_HANDLE handle, uintptr_t hReferenceData,
               DRV_USB_EVENT_CALLBACK eventHandler);

        /* This is a pointer to the Host IRP submit function */
        USB_ERROR (*hostIRPSubmit)(DRV_USB_HOST_PIPE_HANDLE pipeHandle, USB_HOST_IRP * irp);

        /* This is a pointer to the Host IRP Cancel all function */
        void (*hostIRPCancel)(USB_HOST_IRP * irp);

        /* This is pointer to the Host event disable function */
        bool (*hostEventsDisable)(DRV_HANDLE handle);

        /* This is a pointer to the Host event enable function */
        void (*hostEventsEnable)(DRV_HANDLE handle, bool eventContext);

        /* This is a pointer to the Host pipe setup function */
        DRV_USB_HOST_PIPE_HANDLE (*hostPipeSetup)
        (
            DRV_HANDLE client,
            uint8_t deviceAddress,
            USB_ENDPOINT endpointAndDirection,
            uint8_t hubAddress,
            uint8_t hubPort,
            USB_TRANSFER_TYPE pipeType,
            uint8_t bInterval,
            uint16_t wMaxPacketSize,
            USB_SPEED speed
        );

        /* This is a pointer to the Host Pipe Close function */
        void (*hostPipeClose)(DRV_USB_HOST_PIPE_HANDLE pipeHandle);

        /* This is a pointer to the Host Root Hub functions */
        DRV_USB_ROOT_HUB_INTERFACE rootHubInterface;

    } DRV_USB_HOST_INTERFACE;

The DRV_USB_DEVICE_INTERFACE structure, contained in this file, is the common interface for USB Driver Device mode functions. It is a structure of function pointers, pointer to functions that define the Driver Device mode Client functions. The following code example shows this structure and the function pointer it contains.

    // *****************************************************************************
    /* USB Driver Client Functions Interface (For Device Mode)

      Summary:
        Group of function pointers to the USB Driver Device Mode Client Functions.

      Description:
        This structure is a group of function pointers pointing to the USB Driver
        Device Mode Client routines. The USB Driver should export this group of
        functions so that the Device Layer can access the driver functionality.

      Remarks:
        None.
    */typedefstruct
    {
        /* This is a pointer to the driver Open function */
        DRV_HANDLE (*open)(const SYS_MODULE_INDEX drvIndex, const DRV_IO_INTENT intent);

        /* This is pointer to the driver Close function */
        void (*close)(DRV_HANDLE handle);

        /* This is a pointer to the event call back set function */
        void (*eventHandlerSet)(DRV_HANDLE handle, uintptr_t hReferenceData,
               DRV_USB_EVENT_CALLBACK eventHandler);

        /* This is a pointer to the device address set function */
        void (*deviceAddressSet)(DRV_HANDLE handle, uint8_t address);

        /* This is a pointer to the device current speed get function */
        USB_SPEED (*deviceCurrentSpeedGet)(DRV_HANDLE handle);

        /* This is a pointer to the SOF Number get function */
        uint16_t (*deviceSOFNumberGet)(DRV_HANDLE handle);

        /* This is a pointer to the device attach function */
        void (*deviceAttach)(DRV_HANDLE handle);

        /* This is a pointer to the device detach function */
        void (*deviceDetach)(DRV_HANDLE handle);

        /* This is a pointer to the device endpoint enable function */
        USB_ERROR (*deviceEndpointEnable)(DRV_HANDLE handle, USB_ENDPOINT endpoint,
                    USB_TRANSFER_TYPE transferType, uint16_t endpointSize);

        /* This is a pointer to the device endpoint disable function */
        USB_ERROR (*deviceEndpointDisable)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is a pointer to the device endpoint stall function */
        USB_ERROR (*deviceEndpointStall)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is a pointer to the device endpoint stall clear function */
        USB_ERROR (*deviceEndpointStallClear)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is pointer to the device endpoint enable status query function */
        bool (*deviceEndpointIsEnabled)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is pointer to the device endpoint stall status query function */
        bool (*deviceEndpointIsStalled)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is a pointer to the device IRP submit function */
        USB_ERROR (*deviceIRPSubmit)(DRV_HANDLE handle, USB_ENDPOINT endpoint,
                    USB_DEVICE_IRP * irp);

        /* This is a pointer to the device IRP Cancel all function */
        USB_ERROR (*deviceIRPCancelAll)(DRV_HANDLE handle, USB_ENDPOINT endpoint);

        /* This is a pointer to the device remote wakeup start function */
        void (*deviceRemoteWakeupStart)(DRV_HANDLE handle);

        /* This is a pointer to the device remote wakeup stop function */
        void (*deviceRemoteWakeupStop)(DRV_HANDLE handle);

        /* This is a pointer to the device Test mode enter function */
        USB_ERROR (*deviceTestModeEnter)(DRV_HANDLE handle, USB_TEST_MODE_SELECTORS testMode);

    } DRV_USB_DEVICE_INTERFACE;

Both of these structures also contain pointers to General Client functions. The specific PIC32 device USB Driver allocates and initializes such a structure. The following code example shows how the PIC32MX USB Host mode Driver allocates and initializes the DRV_USB_HOST_INTERFACE structure. This code is contained in the

`<install-dir>\framework\driver\usb\usbhs\src\dynamic\drv_usbfs_host.c` file.

    /**********************************************************
     * This structure is a set of pointer to the USBFS driver
     * functions. It is provided to the Host layer as the
     * interface to the driver.
     * *******************************************************/

    DRV_USB_HOST_INTERFACE gDrvUSBFSHostInterface =
    {
        .open = DRV_USBFS_Open,
        .close = DRV_USBFS_Close,
        .eventHandlerSet = DRV_USBFS_ClientEventCallBackSet,
        .hostIRPSubmit = DRV_USBFS_HOST_IRPSubmit,
        .hostIRPCancel = DRV_USBFS_HOST_IRPCancel,
        .hostPipeSetup = DRV_USBFS_HOST_PipeSetup,
        .hostPipeClose = DRV_USBFS_HOST_PipeClose,
        .hostEventsDisable = DRV_USBFS_HOST_EventsDisable,
        .hostEventsEnable = DRV_USBFS_HOST_EventsEnable,
        .rootHubInterface.rootHubPortInterface.hubPortReset = DRV_USBFS_HOST_ROOT_HUB_PortReset,
        .rootHubInterface.rootHubPortInterface.hubPortSpeedGet =
                                                        DRV_USBFS_HOST_ROOT_HUB_PortSpeedGet,
        .rootHubInterface.rootHubPortInterface.hubPortResetIsComplete =
                                                        DRV_USBFS_HOST_ROOT_HUB_PortResetIsComplete,
        .rootHubInterface.rootHubPortInterface.hubPortSuspend = DRV_USBFS_HOST_ROOT_HUB_PortSuspend,
        .rootHubInterface.rootHubPortInterface.hubPortResume = DRV_USBFS_HOST_ROOT_HUB_PortResume,
        .rootHubInterface.rootHubMaxCurrentGet = DRV_USBFS_HOST_ROOT_HUB_MaximumCurrentGet,
        .rootHubInterface.rootHubPortNumbersGet = DRV_USBFS_HOST_ROOT_HUB_PortNumbersGet,
        .rootHubInterface.rootHubSpeedGet = DRV_USBFS_HOST_ROOT_HUB_BusSpeedGet,
        .rootHubInterface.rootHubInitialize = DRV_USBFS_HOST_ROOT_HUB_Initialize,
        .rootHubInterface.rootHubOperationEnable = DRV_USBFS_HOST_ROOT_HUB_OperationEnable,
        .rootHubInterface.rootHubOperationIsEnabled = DRV_USBFS_HOST_ROOT_HUB_OperationIsEnabled,
    };

Similarly, the PIC32MX USB Device mode Driver allocates and initializes the DRV_USB_DEVICE_INTERFACE structure. This can be reviewed in the

`<install-dir>\framework\driver\usb\usbhs\src\dynamic\drv_usbfs_device.c` file.

    /*****************************************************
     * This structure is a pointer to a set of USB Driver
     * Device mode functions. This set is exported to the
     * Device Layer when the Device Layer must use the
     * PIC32MX USB Controller.
     ******************************************************/

    DRV_USB_DEVICE_INTERFACE gDrvUSBFSDeviceInterface =
    {
        .open = DRV_USBFS_Open,
        .close = DRV_USBFS_Close,
        .eventHandlerSet = DRV_USBFS_ClientEventCallBackSet,
        .deviceAddressSet = DRV_USBFS_DEVICE_AddressSet,
        .deviceCurrentSpeedGet = DRV_USBFS_DEVICE_CurrentSpeedGet,
        .deviceSOFNumberGet = DRV_USBFS_DEVICE_SOFNumberGet,
        .deviceAttach = DRV_USBFS_DEVICE_Attach,
        .deviceDetach = DRV_USBFS_DEVICE_Detach,
        .deviceEndpointEnable = DRV_USBFS_DEVICE_EndpointEnable,
        .deviceEndpointDisable = DRV_USBFS_DEVICE_EndpointDisable,
        .deviceEndpointStall = DRV_USBFS_DEVICE_EndpointStall,
        .deviceEndpointStallClear = DRV_USBFS_DEVICE_EndpointStallClear,
        .deviceEndpointIsEnabled = DRV_USBFS_DEVICE_EndpointIsEnabled,
        .deviceEndpointIsStalled = DRV_USBFS_DEVICE_EndpointIsStalled,
        .deviceIRPSubmit = DRV_USBFS_DEVICE_IRPSubmit,
        .deviceIRPCancelAll = DRV_USBFS_DEVICE_IRPCancelAll,
        .deviceRemoteWakeupStop = DRV_USBFS_DEVICE_RemoteWakeupStop,
        .deviceRemoteWakeupStart = DRV_USBFS_DEVICE_RemoteWakeupStart,
        .deviceTestModeEnter = NULL
    };

A pointer to the DRV_USB_HOST_INTERFACE structure is passed to the USB Host Stack as part of USB Host Stack initialization. The following code example shows how this is done.

    /********************************************************************************
    * This is a table of the USB Host mode drivers that this application will
     * support. Also contained in the driver index. In this example, the
     * application will want to use instance 0 of the PIC32MX USB Full-Speed driver.
     * *****************************************************************************/
    const USB_HOST_HCD hcdTable =
    {
        .drvIndex = DRV_USBFS_INDEX_0,
        .hcdInterface = DRV_USBFS_HOST_INTERFACE
    };

    /* Here the pointer to the USB Driver Common Interface is provided to the USB
     * Host Layer via the hostControllerDrivers member of the Host Layer
     * Initialization data structure. */
    const USB_HOST_INIT usbHostInitData =
    {
        .nTPLEntries = 1 ,
        .tplList = (USB_HOST_TPL_ENTRY *)USBTPList,
        .hostControllerDrivers = (USB_HOST_HCD *)&hcdTable

    };

A pointer to the DRV_USB_DEVICE_INTERFACE structure is passed to the USB Device Stack as part of the USB Device Stack initialization. The Host Stack and Device Stack then access the driver functions through the function pointers contained in these structures.

The Driver General Client, Host mode and Device mode Client functions are described in this section. Any references to a USB Driver Client in the following sections, implies the client is a USB Host Stack and/or the USB Device Stack.

## Topics

Name | Description
Driver Host Mode Client Functions | Provides information on the Host mode Client functions for the USB Driver.
Driver Device Mode Client Functions | Provides information on the USB Driver Device mode Client functions.
Driver General Client Functions | Provides information on the General Client functions for the USB Driver.
Opening the Driver | Provides information and examples for opening the driver.
USB Driver Host Mode Operation | Provides information on Host mode operation.
USB Driver Device Mode Operation | Provides information on Device mode operation.