# How to use the USB PCD HAL module driver
## The USB Peripheral Controller Driver (PCD) HAL driver can be used as follows:
1. Declare a hal_pcd_handle_t handle structure, for example:
hal_pcd_handle_t gh_pcd_usb_otg_fs;
hal_pcd_handle_t gh_pcd_usb_otg_hs;
hal_pcd_handle_t gh_pcd_usb_drd_fs;
2. Initialize the USB PCD low-level resources:
- USB PCD interface clock configuration:
- Enable USB peripheral clock.
- USB PCD interface power configuration:
- Enable USB peripheral VddUSB power supply if applicable.
- Enable USB HS transceiver power supply if applicable.
- USB device 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_PCD_IRQHandler():
- Set the USB PCD interrupt priority.
- Enable the USB IRQ channel in the NVIC.
3. Initialize the USB PCD driver with HAL_PCD_Init() and by selecting an instance, for example:
HAL_PCD_Init(&gh_pcd_usb_otg_fs, HAL_PCD_OTG_FS);
HAL_PCD_Init(&gh_pcd_usb_otg_hs, HAL_PCD_OTG_HS);
HAL_PCD_Init(&gh_pcd_usb_drd_fs, HAL_PCD_DRD_FS);
- Declare a hal_pcd_config_t structure, for example:
hal_pcd_config_t config_pcd_usb_otg_fs;
hal_pcd_config_t config_pcd_usb_otg_hs;
hal_pcd_config_t config_pcd_usb_drd_fs;
- In the configuration structure,
program the PHY interface, core speed, and other parameters as required.
- Apply the configuration with
HAL_PCD_SetConfig(&gh_pcd_usb_otg_fs, &config_pcd_usb_otg_fs);
HAL_PCD_SetConfig(&gh_pcd_usb_otg_hs, &config_pcd_usb_otg_hs);
HAL_PCD_SetConfig(&gh_pcd_usb_drd_fs, &config_pcd_usb_drd_fs);
- Configure required USB device endpoints.
## Callbacks definition:
By default, all callbacks are initialized to their corresponding default weak functions.
When the compilation define USE_HAL_PCD_REGISTER_CALLBACKS is set to 1U, the user can configure dynamically the
driver callbacks using the Callback registration functions:
| Default callback weak function | Callback registration function
|--------------------------------------------------|------------------------------------------------------------------
| HAL_PCD_SofCallback() | HAL_PCD_RegisterSofCallback()
| HAL_PCD_SetupStageCallback() | HAL_PCD_RegisterSetupCallback()
| HAL_PCD_ResetCallback() | HAL_PCD_RegisterResetCallback()
| HAL_PCD_SuspendCallback() | HAL_PCD_RegisterSuspendCallback()
| HAL_PCD_ResumeCallback() | HAL_PCD_RegisterResumeCallback()
| HAL_PCD_ConnectCallback() | HAL_PCD_RegisterConnectCallback()
| HAL_PCD_DisconnectCallback() | HAL_PCD_RegisterDisconnectCallback()
| HAL_PCD_DataOutStageCallback() | HAL_PCD_RegisterDataOutStageCallback()
| HAL_PCD_DataInStageCallback() | HAL_PCD_RegisterDataInStageCallback()
| HAL_PCD_ISOOUTIncompleteCallback() | HAL_PCD_RegisterIsoOutIncpltCallback()
| HAL_PCD_ISOINIncompleteCallback() | HAL_PCD_RegisterIsoInIncpltCallback()
| HAL_PCD_ErrorCallback() | HAL_PCD_RegisterErrorCallback()
| HAL_PCD_BcdCallback() | HAL_PCD_RegisterBcdCallback()
| HAL_PCD_LpmCallback() | HAL_PCD_RegisterLpmCallback()
## Configuration inside the USB PCD 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_PCD_MODULE | from hal_conf.h | 1 | Enable the HAL USB PCD module.
| USE_HAL_PCD_REGISTER_CALLBACKS | from hal_conf.h | 0 | Enable the register callbacks.
| USE_HAL_PCD_USB_EP_TYPE_ISOC | from hal_conf.h | 1 | Enable support for isochronous endpoints.
| USE_HAL_PCD_USB_BCD | from hal_conf.h | 0 | Enable USB Battery Charging Detection support.
| USE_HAL_PCD_USB_LPM | from hal_conf.h | 0 | Enable USB Link Power Management support.
| USE_HAL_PCD_USB_DOUBLE_BUFFER | from hal_conf.h | 1 | Enable double-buffering for USB transfers.
| USE_HAL_PCD_MAX_ENDPOINT_NB | from hal_conf.h | 16 | Maximum number of USB PCD endpoints.
| USE_HAL_PCD_USER_DATA | from hal_conf.h | 0 | Add user data inside HAL USB PCD handle.
| USE_HAL_PCD_GET_LAST_ERRORS | from hal_conf.h | 0 | Add error value inside HAL USB PCD handle.
| USE_HAL_CHECK_PARAM | from hal_conf.h | 0 | Enable checking of PCD API parameters at runtime.