[活动专区] 【AT-START-F405测评】USB_Host cdc在freertos下速度测试及优化

[复制链接]
 楼主| 发表于 2024-5-19 21:36 | 显示全部楼层 |阅读模式
本帖最后由 九子帝王 于 2024-5-19 21:46 编辑

#申请原创#
拿到板子之后就上手测试了cdc的速度,device端使用的手头另外的ic
打开工程”**\project\at_start_f405\examples\usb_host\cdc_demo“
修改keil中的define,将USB_OTG_HS改为USB_OTG_FS
1.png
烧写并运行代码,使用usb抓包工具抓包如图,可以看出每帧发送的数据量在240-360 byte左右,换算成秒即为240k-360k byte之间
2.png
但是我需要使用freertos,将usb作为其中的一个任务,那么此时就会产生要在while(1)的循环中增加vTaskDelay()用以任务调度
  1. while(1)
  2.     {
  3.     vTaskDelay(1);
  4.     usbh_loop_handler(&otg_core_struct.host);
  5.     /* if press user key, host send data to device */
  6.     if(at32_button_press() == USER_BUTTON)
  7.     {
  8.         cdc_start_transmission(&otg_core_struct.host, (uint8_t *)tx_data, 60);
  9.         cdc_start_reception(&otg_core_struct.host, (uint8_t *)rx_data, 64);
  10.     }
  11.     }
那么在这种情况下速度就会降低至一帧64-128 byte
3.png
尝试将
  1. cdc_start_transmission(&otg_core_struct.host, (uint8_t *)tx_data, 60);
两句中的数据量增加,并没有什么作用,追踪代码发现是由于实际调用的发送函数会将一次传输的数量限制在 ”out_endpoint_size“之内 4.png
进一步追踪发现out_endpoint_size即为设备端端点长度64
5.png 而调用 cdc_process_transmission() 的正是上文中的 usbh_loop_handler() 函数。至此,在freertos下速度上限被卡在了任务调度周期上。
查看F405的用户手册,发现有FIFO空中断可以使用,且此中断收寄存器 PTXFEMPLVL 的控制,可以选择半空还是全空中断
6.png
围绕半空中段,最终改造demo程序,提升传输速度
一、开启半空中断
usbh_core.c

在函数 void usbh_resume(usbh_core_type *uhost) 中增加 USB_OTG_NPTXFEMP_INT 语句
  1.   /* set global interrut mask */
  2.   usbx->gintmsk = USB_OTG_SOF_INT  | USB_OTG_NPTXFEMP_INT |
  3.                          USB_OTG_USBSUSP_INT | USB_OTG_PRT_INT |
  4.                          USB_OTG_HCH_INT | USB_OTG_INCOMISOIN_INT |
  5.                          USB_OTG_INCOMPIP_INCOMPISOOUT_INT | USB_OTG_WKUP_INT |
  6.                          USB_OTG_DISCON_INT;
二、添加半空中断传输入口
usbh_int.c
  1. void usbh_irq_handler(otg_core_type *otgdev)
  2. {
  3.   otg_global_type *usbx = otgdev->usb_reg;
  4.   usbh_core_type *uhost = &otgdev->host;
  5.   uint32_t intsts = usb_global_get_all_interrupt(usbx);

  6.   if(usbx->gintsts_bit.curmode == 1)
  7.   {
  8.     if(intsts & USB_OTG_HCH_FLAG)
  9.     {
  10.       usbh_hch_handler(uhost);
  11.       usb_global_clear_interrupt(usbx, USB_OTG_HCH_FLAG);
  12.     }
  13.     if(intsts & USB_OTG_SOF_FLAG)
  14.     {
  15.       usbh_sof_handler(uhost);
  16.       usb_global_clear_interrupt(usbx, USB_OTG_SOF_FLAG);
  17.     }
  18.     if(intsts & USB_OTG_NPTXFEMP_FLAG)
  19.     {
  20.       extern void usbh_txempty_handler(usbh_core_type *uhost);
  21.       usbh_txempty_handler(uhost);
  22.       usb_global_clear_interrupt(usbx, USB_OTG_NPTXFEMP_FLAG);
  23.     }

三、修改传输和中断函数
usbh_cdc_class.c

添加中断函数
  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] usb host tx empty handler
  3. * @param uhost: to the structure of usbh_core_type
  4. * @retval none
  5. */
  6. void usbh_txempty_handler(usbh_core_type *uhost)
  7. {
  8.     if(uhost->global_state != USBH_CLASS) {
  9.         uhost->usb_reg->gintmsk_bit.nptxfempmsk = 0;
  10.         return;
  11.     }
  12.     cdc_process_transmission(uhost);
  13. }

修改发送处理函数
  1. /**
  2.   * @brief  usb host cdc class process transmission handler
  3.   * @param  uhost: to the structure of usbh_core_type
  4.   * @retval status: usb_sts_type status
  5.   */
  6. static void cdc_process_transmission(usbh_core_type *uhost)
  7. {
  8.   usbh_core_type *puhost = (usbh_core_type *)uhost;
  9.   usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;

  10.   switch(pcdc->data_tx_state)
  11.   {
  12.     case CDC_SEND_DATA:
  13.       if(pcdc->tx_len > pcdc->data_interface.out_endpoint_size)
  14.       {
  15.         usbh_bulk_send(puhost, pcdc->data_interface.out_channel, (uint8_t*)pcdc->tx_data, pcdc->data_interface.out_endpoint_size);
  16.       }
  17.       else
  18.       {
  19.         usbh_bulk_send(puhost, pcdc->data_interface.out_channel, (uint8_t*)pcdc->tx_data, pcdc->tx_len);
  20.       }
  21.       pcdc->data_tx_state = CDC_SEND_DATA_WAIT;
  22.     break;
  23.    
  24.     case CDC_SEND_DATA_WAIT:
  25.       if(uhost->urb_state[pcdc->data_interface.out_channel] == URB_DONE)
  26.       {
  27.         if(pcdc->tx_len > pcdc->data_interface.out_endpoint_size)
  28.         {
  29.           pcdc->tx_len -= pcdc->data_interface.out_endpoint_size;
  30.           pcdc->tx_data += pcdc->data_interface.out_endpoint_size;
  31.           pcdc->data_tx_state = CDC_SEND_DATA;
  32.         }
  33.         else if(pcdc->tx_len == pcdc->data_interface.out_endpoint_size)  // cdc acm mode An empty packet must be sent to indicate that the transmission is complete
  34.         {
  35.             pcdc->tx_len = 0;
  36.             pcdc->data_tx_state = CDC_SEND_DATA;
  37.         }
  38.         else
  39.         {
  40.           pcdc->tx_len = 0;
  41.           pcdc->data_tx_state = CDC_IDLE;
  42.           puhost->usb_reg->gintmsk_bit.nptxfempmsk = 0;
  43.           cdc_transmit_complete(uhost);
  44.         }
  45.       }
  46.       else if( uhost->urb_state[pcdc->data_interface.out_channel] == URB_NOTREADY)
  47.       {
  48.         pcdc->data_tx_state = CDC_SEND_DATA;
  49.       }
  50.     break;
  51.    
  52.     default:
  53.     break;
  54.   }
  55. }

修改发送开始函数
  1. /**
  2.   * @brief  usb host cdc class start transmission handler
  3.   * @param  uhost: to the structure of usbh_core_type
  4.   * @param  data: tx data pointer
  5.   * @param  len: tx data len
  6.   * @retval status: usb_sts_type status
  7.   */
  8. void cdc_start_transmission(usbh_core_type *uhost, uint8_t *data, uint32_t len)
  9. {
  10.   usbh_core_type *puhost = (usbh_core_type *)uhost;
  11.   usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
  12.   if(pcdc->data_tx_state == CDC_IDLE)
  13.   {
  14.     pcdc->data_tx_state = CDC_SEND_DATA;
  15.     pcdc->state = CDC_TRANSFER_DATA;
  16.     pcdc->tx_data = data;
  17.     pcdc->tx_len = len;
  18.     puhost->usb_reg->gintmsk_bit.nptxfempmsk = 1;
  19.   }
  20. }

修改接收处理函数
  1. uint16_t in_fifo_size = USBH_RX_FIFO_SIZE * 4;
  2. /**
  3.   * @brief  usb host cdc class process reception handler
  4.   * @param  uhost: to the structure of usbh_core_type
  5.   * @retval status: usb_sts_type status
  6.   */
  7. static void cdc_process_reception(usbh_core_type *uhost)
  8. {
  9.   usbh_core_type *puhost = (usbh_core_type *)uhost;
  10.   usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
  11.   uint32_t len = 0;
  12.    
  13.   switch(pcdc->data_rx_state)
  14.   {
  15.     case CDC_RECEIVE_DATA:
  16.       if(pcdc->rx_len > in_fifo_size)
  17.           usbh_bulk_recv(puhost, pcdc->data_interface.in_channel, (uint8_t *)pcdc->rx_data, in_fifo_size);
  18.       else
  19.           usbh_bulk_recv(puhost, pcdc->data_interface.in_channel, (uint8_t *)pcdc->rx_data, pcdc->rx_len);
  20.       pcdc->data_rx_state = CDC_RECEIVE_DATA_WAIT;
  21.     break;
  22.    
  23.     case CDC_RECEIVE_DATA_WAIT:
  24.       if(uhost->urb_state[pcdc->data_interface.in_channel] == URB_DONE)
  25.       {
  26.         len = uhost->hch[pcdc->data_interface.in_channel].trans_count;
  27.         if(pcdc->rx_len > len)
  28.         {
  29.           pcdc->rx_len -= len;
  30.           pcdc->rx_data += len;
  31.           pcdc->data_rx_state = CDC_RECEIVE_DATA;
  32.         }
  33.         else
  34.         {
  35.           pcdc->rx_len = 0;
  36.           pcdc->data_rx_state = CDC_IDLE;
  37.           cdc_receive_complete(uhost);
  38.          
  39.         }
  40.       }

  41.     break;
  42.    
  43.     default:
  44.     break;
  45.   }
  46. }

四、修改传输完成回调函数,添加usb任务和速度测试任务
  1. TaskHandle_t usbh_handler, usbh_speed_print_handler;
  2. otg_core_type otg_core_struct;
  3. void usb_clock48m_select(usb_clk48_s clk_s);
  4. #define CDC_SIZE 2048
  5. uint8_t tx_data[CDC_SIZE] = {0};
  6. uint8_t rx_data[CDC_SIZE];
  7. uint8_t  test = 0, cmplt = 0;
  8. uint32_t speed = 0;

  9. void usbh_speed_print_handle(void *pvParameters)
  10. {
  11.     TickType_t xLastWakeTime = xTaskGetTickCount ();
  12.     while(1)
  13.     {
  14.         xTaskDelayUntil( &xLastWakeTime, 1000);
  15.         if(1 == test)
  16.             printf("reception speed:%d bytes/s\r\n", speed);
  17.         else if(2 == test)
  18.             printf("transmission speed:%d bytes/s\r\n", speed);
  19.         speed = 0;
  20.     }
  21. }
  22. void usbh_speed_test_handle(void *pvParameters)
  23. {
  24.     nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  25.     /* enable otg clock */
  26.     crm_periph_clock_enable(OTG_CLOCK, TRUE);

  27.     /* select usb 48m clcok source */
  28.     usb_clock48m_select(USB_CLK_HEXT);

  29.     /* enable otg irq */
  30.     nvic_irq_enable(OTG_IRQ, 0, 0);

  31.     /* init usb */
  32.     usbh_init(&otg_core_struct,
  33.                 USB_SPEED_CORE_ID,
  34.                 USB_ID,
  35.                 &uhost_cdc_class_handler,
  36.                 &usbh_user_handle);
  37.     for(uint16_t i = 0; i < CDC_SIZE; i++)
  38.         tx_data[i] = i/64;
  39.     while(1)
  40.     {
  41.         vTaskDelay(1);
  42.         usbh_loop_handler(&otg_core_struct.host);
  43.         if(cmplt) {
  44.             cmplt = 0;
  45.             if(1 == test)
  46.                 cdc_start_reception(&otg_core_struct.host, (uint8_t *)rx_data, CDC_SIZE);
  47.             if(2 == test)
  48.                 cdc_start_transmission(&otg_core_struct.host, (uint8_t *)tx_data, CDC_SIZE);
  49.         }
  50.         
  51.         /* if press user key, host send data to device */
  52.         if(at32_button_state() != RESET)
  53.         {
  54.             if(0 == test){
  55.                 test = 1;
  56.                 cdc_start_reception(&otg_core_struct.host, (uint8_t *)rx_data, CDC_SIZE);
  57.             } else if(1 == test){
  58.                 test = 2;
  59.                 cdc_start_transmission(&otg_core_struct.host, (uint8_t *)tx_data, CDC_SIZE);
  60.             }
  61.             else{
  62.                 test = 0;
  63.             }
  64.             while(at32_button_state() != RESET)
  65.                 vTaskDelay(1);
  66.         }
  67.     }
  68. }

  69. /**
  70.   * @brief  usb host cdc class transmit complete
  71.   * @param  uhost: to the structure of usbh_core_type
  72.   * @retval status: usb_sts_type status
  73.   */
  74. void cdc_transmit_complete(usbh_core_type *uhost)
  75. {
  76.     speed += CDC_SIZE;
  77.     cmplt = 1;
  78. }

  79. /**
  80.   * @brief  usb host cdc class reception complete
  81.   * @param  uhost: to the structure of usbh_core_type
  82.   * @retval status: usb_sts_type status
  83.   */
  84. void cdc_receive_complete(usbh_core_type *uhost)
  85. {
  86.     speed += CDC_SIZE;
  87.     cmplt = 1;
  88. }

  89. void usbh_test_init(void)
  90. {
  91.     /* creat usb host cdc speed test task*/
  92.     if (xTaskCreate((TaskFunction_t)usbh_speed_test_handle,
  93.                     (const char *)"usbh_speed_test_task",
  94.                     (uint16_t)1024,
  95.                     (void *)NULL,
  96.                     (UBaseType_t)2,
  97.                     (TaskHandle_t *)&usbh_handler) != pdPASS){
  98.         printf("create usbh_speed_test_task failed!\r\n");
  99.     } else {
  100.         printf("create usbh_speed_test_task success!\r\n");
  101.     }
  102.     if (xTaskCreate((TaskFunction_t)usbh_speed_print_handle,
  103.                     (const char *)"usbh_speed_print_task",
  104.                     (uint16_t)512,
  105.                     (void *)NULL,
  106.                     (UBaseType_t)3,
  107.                     (TaskHandle_t *)&usbh_speed_print_handler) != pdPASS){
  108.         printf("create usbh_speed_print_task failed!\r\n");
  109.     } else {
  110.         printf("create usbh_speed_print_task success!\r\n");
  111.     }
  112. }

五、测试结果
由于只针对发送做了半空中断处理,接收功能只做了增加单次传输的限制更改,导致接收速度没有达到较大的提升
测试结果如下
7.png
9.png
8.png
可以看到目前发送速度已经达到1Mbyte/s左右
工程 usbHost_freertos.zip (6.27 MB, 下载次数: 2)


您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

18

帖子

2

粉丝
快速回复 返回顶部 返回列表