前面我们准备了打印输出部分的实现,现在开始就可以基于此进行USB的调试了。
添加USB相关代码到现有的代码上
USB相关代码,PCD的驱动
Stm32fxx_it.c中usb中断处理
void OTG_FS_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd);
}
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] This function handles USB Handler.
* @param None
* @retval None
*/
void OTG_FS_WKUP_IRQHandler(void)
{
if((&hpcd)->Init.low_power_enable)
{
/* Reset SLEEPDEEP bit of Cortex System Control Register */
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
SystemClockConfig_STOP();
/* Ungate PHY clock */
__HAL_PCD_UNGATE_PHYCLOCK((&hpcd));
}
/* Clear EXTI pending Bit*/
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
}
Inc\stm32f7xx_hal_conf.h中
定义#define HAL_PCD_MODULE_ENABLED使用PCD库代码
Main函数初始化
int main(void)
{
/* Configure the MPU attributes */
MPU_Config();
/* Enable the CPU Cache */
CPU_CACHE_Enable();
/* STM32F7xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 216 MHz */
SystemClock_Config();
/* Init Device Library */
USBD_Init(&USBD_Device, &HID_Desc, 0);
/* Add Supported Class */
USBD_RegisterClass(&USBD_Device, USBD_HID_CLASS);
/* Start Device Process */
USBD_Start(&USBD_Device);
bsp_uart_init();
uint8_t tx_buffer[2]={0xAA,0x55};
static uint32_t pretick;
/* Infinite loop */
pretick = HAL_GetTick();
debug_set_level(DEBUG_TAG_SETUP, DEBUG_LEVEL_INFO);
while (1)
{
uart_debug_send(16);
shell_exec_shellcmd();
}
}
Main中使能打印输出
debug_set_level(DEBUG_TAG_SETUP, DEBUG_LEVEL_INFO);
usbd_core.c中USBD_LL_SetupStage中添加如下日志输出,打印8字节的SETUP内容
do_debug(DEBUG_TAG_SETUP,DEBUG_LEVEL_INFO,"[setup]:%#x %#x %#02x %#02x %#02x\r\n",pdev->request.bmRequest,
pdev->request.bRequest,pdev->request.wValue,pdev->request.wIndex,pdev->request.wLength);
看到整个枚举过程打印如下
比如第一条0x80 0x06 0x100 00 0x40即获取设备描述符
第二条0 0x5 0x4 00 00即设置地址
可以清晰的看出整个枚举过程
磨刀不误砍柴工,工欲善其事必先利其器,有了前面的准备工作,准备了好用的命令行交互和打印输出,可以方便后面USB的开发调试,提高效率。