# How to use the USB HCD HAL module driver
## The USB Host Controller Driver (HCD) HAL driver can be used as follows:
1. Declare a hal_hcd_handle_t handle structure, for example:
hal_hcd_handle_t gh_hcd_usb_otg_fs;
hal_hcd_handle_t gh_hcd_usb_otg_hs;
hal_hcd_handle_t gh_hcd_usb_drd_fs;
2. Initialize the USB HCD low-level resources:
- USB HCD interface clock configuration:
- Enable USB peripheral clock.
- USB HCD interface Power configuration:
- Enable USB peripheral VddUSB power supply if applicable.
- Enable USB HS transceiver power supply if applicable.
- USB host pins configuration:
USB data pins are automatically configured by the hardware during USB peripheral initialization;
no additional user action is required.
- NVIC configuration for interrupt handling with HAL_HCD_IRQHandler():
- Set the USB HCD interrupt priority.
- Enable the USB IRQ channel in the NVIC.
3. Initialize the USB HCD driver with HAL_HCD_Init() and by selecting an instance, for example:
HAL_HCD_Init(&gh_hcd_usb_otg_fs, HAL_HCD_OTG_FS);
HAL_HCD_Init(&gh_hcd_usb_otg_hs, HAL_HCD_OTG_HS);
HAL_HCD_Init(&gh_hcd_usb_drd_fs, HAL_HCD_DRD_FS);
- Declare a hal_hcd_config_t structure, for example:
hal_hcd_config_t config_hcd_usb_otg_fs;
hal_hcd_config_t config_hcd_usb_otg_hs;
hal_hcd_config_t config_hcd_usb_drd_fs;
- In the configuration structure,
program the PHY interface, core speed, and other parameters as required.
- Apply the configuration with
HAL_HCD_SetConfig(&gh_hcd_usb_otg_fs, &config_hcd_usb_otg_fs);
HAL_HCD_SetConfig(&gh_hcd_usb_otg_hs, &config_hcd_usb_otg_hs);
HAL_HCD_SetConfig(&gh_hcd_usb_drd_fs, &config_hcd_usb_drd_fs);
## Callbacks definition:
By default, all callbacks are initialized to their corresponding default weak functions.
When the compilation define USE_HAL_HCD_REGISTER_CALLBACKS is set to 1U, the user can configure dynamically the
driver callbacks using the Callback registration function:
| Default callback weak function | Callback registration function
|--------------------------------------------------|------------------------------------------------------------------
| HAL_HCD_SofCallback() | HAL_HCD_RegisterSofCallback()
| HAL_HCD_PortConnectCallback() | HAL_HCD_RegisterPortConnectCallback()
| HAL_HCD_PortDisconnectCallback() | HAL_HCD_RegisterPortDisconnectCallback()
| HAL_HCD_PortEnabledCallback() | HAL_HCD_RegisterPortEnabledCallback()
| HAL_HCD_PortDisabledCallback() | HAL_HCD_RegisterPortDisabledCallback()
| HAL_HCD_PortSuspendCallback() | HAL_HCD_RegisterPortSuspendCallback()
| HAL_HCD_PortResumeCallback() | HAL_HCD_RegisterPortResumeCallback()
| HAL_HCD_ChannelNotifyURBChangeCallback() | HAL_HCD_RegisterChannelNotifyURBChangeCallback()
| HAL_HCD_ErrorCallback() | HAL_HCD_RegisterErrorCallback()
## Configuration inside the USB HCD driver:
| Config defines | Description | Default value | Note
|--------------------------------|-----------------|---------------|--------------------------------------------------
| USE_ASSERT_DBG_PARAM | from IDE | NA | Enable the params assert.
| USE_ASSERT_DBG_STATE | from IDE | NA | Enable the state assert.
| USE_HAL_HCD_MODULE | from hal_conf.h | 1 | Enable the HAL USB HCD module.
| USE_HAL_HCD_REGISTER_CALLBACKS | from hal_conf.h | 0 | Enable the register callbacks.
| USE_HAL_HCD_MAX_CHANNEL_NB | from hal_conf.h | 16 | Maximum number of USB HCD channels.
| USE_HAL_HCD_USB_EP_TYPE_ISOC | from hal_conf.h | 1 | Enable support for isochronous endpoints.
| USE_HAL_HCD_USB_HUB_HS_SPLIT | from hal_conf.h | 1 | Enable high-speed split transactions for hubs.
| USE_HAL_HCD_USB_DOUBLE_BUFFER | from hal_conf.h | 1 | Enable double-buffering for USB transfers.
| USE_HAL_HCD_USER_DATA | from hal_conf.h | 0 | Add user data inside HAL USB HCD handle.
| USE_HAL_HCD_GET_LAST_ERRORS | from hal_conf.h | 0 | Add error value inside HAL USB HCD handle.
| USE_HAL_CHECK_PARAM | from hal_conf.h | 0 | Enable checking of HCD API parameters at runtime.