qinyunti 发表于 2023-10-30 23:18

【ST多款开发板返场测评】STM32F767 Nucleo-144 HID设备枚举过程调试

<div class='showpostmsg'><h1><b>前言</b></h1>

<p >前面我们准备了打印输出部分的实现,现在开始就可以基于此进行USB的调试了。</p>

<p >&nbsp;</p>

<h1 ><b>过程</b></h1>

<p >添加USB相关代码到现有的代码上</p>

<p >USB相关代码,PCD的驱动</p>

<p > &nbsp;</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >Stm32fxx_it.c中usb中断处理</p>

<pre>
<code class="language-cpp">void OTG_FS_IRQHandler(void)

{

  HAL_PCD_IRQHandler(&amp;hpcd);

}



/**

  * @brief  This function handles USB Handler.

  * @param  None

  * @retval None

  */

void OTG_FS_WKUP_IRQHandler(void)

{

  if((&amp;hpcd)-&gt;Init.low_power_enable)

  {

    /* Reset SLEEPDEEP bit of Cortex System Control Register */

    SCB-&gt;SCR &amp;= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));

    

    SystemClockConfig_STOP();

    

    /* Ungate PHY clock */

     __HAL_PCD_UNGATE_PHYCLOCK((&amp;hpcd));

  }

  /* Clear EXTI pending Bit*/

  __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();

}</code></pre>

<div style="page-break-after:always"><span style="display: none;">&nbsp;</span></div>

<p>&nbsp;</p>

<p >Inc\stm32f7xx_hal_conf.h中</p>

<p >定义#define HAL_PCD_MODULE_ENABLED使用PCD库代码</p>

<p >&nbsp;</p>

<p >Main函数初始化</p>

<pre>
<code class="language-cpp">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(&amp;USBD_Device, &amp;HID_Desc, 0);

  

  /* Add Supported Class */

  USBD_RegisterClass(&amp;USBD_Device, USBD_HID_CLASS);

  

  /* Start Device Process */

  USBD_Start(&amp;USBD_Device);



bsp_uart_init();

  uint8_t tx_buffer={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();

  }

}</code></pre>

<h1 ><b>测试</b></h1>

<p >Main中使能打印输出</p>

<p >debug_set_level(DEBUG_TAG_SETUP, DEBUG_LEVEL_INFO);</p>

<p >&nbsp;</p>

<div style="page-break-after:always"><span style="display: none;">&nbsp;</span></div>

<p>&nbsp;</p>

<p >&nbsp;</p>

<p >usbd_core.c中USBD_LL_SetupStage中添加如下日志输出,打印8字节的SETUP内容</p>

<p >do_debug(DEBUG_TAG_SETUP,DEBUG_LEVEL_INFO,&quot;:%#x %#x %#02x %#02x %#02x\r\n&quot;,pdev-&gt;request.bmRequest,</p>

<p >pdev-&gt;request.bRequest,pdev-&gt;request.wValue,pdev-&gt;request.wIndex,pdev-&gt;request.wLength);</p>

<p >&nbsp;</p>

<p >看到整个枚举过程打印如下</p>

<p >比如第一条0x80 0x06 0x100 00 0x40即获取设备描述符</p>

<p >第二条0 0x5 0x4 00 00即设置地址</p>

<p >可以清晰的看出整个枚举过程</p>

<p > &nbsp;</p>

<h1 ><b>总结</b></h1>

<p >磨刀不误砍柴工,工欲善其事必先利其器,有了前面的准备工作,准备了好用的命令行交互和打印输出,可以方便后面USB的开发调试,提高效率。</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【ST多款开发板返场测评】STM32F767 Nucleo-144 HID设备枚举过程调试