STM32官方USB自定义HID类修改收发64字节

https://my.st.com/content/ccc/resource/technical/software/firmware/44/b1/92/be/d3/26/4e/df/stm32_f105-07_f2_f4_usb-host-device_lib.zip/files/stm32_f105-07_f2_f4_usb-host-device_lib.zip/jcr:content/translations/en.stm32_f105-07_f2_f4_usb-host-device_lib.zip

 

1、官网下载最新的STM32F105/7, STM32F2 and STM32F4 USB on-the-go Host and device library

使用MDK5XX

打开工程,路径\Project\USB_Device_Examples\CustomHID\MDK-ARM

2、工程选择 STM324xG-EVAL_USBD-FS

 

3、按实际更改芯片型号,这里是F407ZG。

   按实际修改外晶体频率。这里system_stm32fxx.c文件中修改 #define PLL_M      8

4、官方例子程序是通过按键中断和AD采集DMA中断事件完成数据发送。

   这里搜索void USBD_USR_Init(void) 注释掉ADC初始化代码,避免进入中断发送。

//  ADC_Configuration();

5、修改设备描述

找到usbd_customhid_core.c

    __ALIGN_BEGIN static uint8_t CustomHID_ReportDesc[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =

{

  0x05, 0xFF,                    // USAGE_PAGE(User define)

    0x09, 0xFF,                    // USAGE(User define)

    0xa1, 0x01,                    // COLLECTION (Application)

 

    0x05, 0x01,                    // USAGE_PAGE(1)

    0x19, 0x00,                    //   USAGE_MINIMUM(0)

    0x29, 0xFF,                    //   USAGE_MAXIMUM(255)

    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)

    0x25, 0xFF,                    //   LOGICAL_MAXIMUM (255)

    0x75, 0x08,                    //   REPORT_SIZE (8)

    0x95, 0x40,                    //   REPORT_COUNT (64)

    0x81, 0x02,                    //   INPUT (Data,Var,Abs)

 

    0x05, 0x02,                    // USAGE_PAGE(2)

    0x19, 0x00,                    //   USAGE_MINIMUM (0)

    0x29, 0xFF,                    //   USAGE_MAXIMUM (255)

    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)

    0x25, 0xFF,                    //   LOGICAL_MAXIMUM (255)

    0x95, 0x08,                    //   REPORT_COUNT (8)

    0x75, 0x02,                    //   REPORT_SIZE (64)

    0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)

 

    0xc0                           // END_COLLECTION

 

};

usbd_customhid_core.h 中#define USBD_CUSTOM_HID_REPORT_DESC_SIZE     39 //163

 

6、修改传输为64字节

在usbd_conf.h中

#define HID_IN_PACKET                64

#define HID_OUT_PACKET               64

 

修改Report_buf数组,全局搜索。

在usbd_customhid_core.c中

uint8_t Report_buf[64];

__ALIGN_BEGIN uint8_t Report_buf[64] __ALIGN_END;

uint8_t  USBD_CUSTOM_HID_DataOut (void  *pdev,

                                  uint8_t epnum)

{

  DCD_EP_PrepareRx(pdev,HID_IN_EP,Report_buf,64);

  

  return USBD_OK;

}

 

7、main()中添加收发测试代码

extern uint8_t Report_buf[];

u8 buf[64]={0x07,0x34};

int main(void)

{

  USBD_Init(&USB_OTG_dev,

#ifdef USE_USB_OTG_HS

            USB_OTG_HS_CORE_ID,

#else

            USB_OTG_FS_CORE_ID,

#endif

            &USR_desc, &USBD_CUSTOMHID_cb, &USR_cb);

  

  while (1)

  {

 u32 i=60000000;

 while(i--);  //大约2秒

 

 if ( USB_OTG_dev.dev.device_status == USB_OTG_CONFIGURED ) //这个可以去掉,里面函数已经实现,这里为了测试。

 {

USBD_CUSTOM_HID_SendReport(&USB_OTG_dev, buf, 64);

 }

 if( Report_buf[0]>0 ) //接收缓冲,64字节。

 {

  if(Report_buf[1]>0)

{

buf[0]=Report_buf[0];

buf[1]=Report_buf[1];

Report_buf[0]=0;

Report_buf[1]=0;

}

 }

  }

}

调试时,插上USB线后PC端USB测试软件再打开设备,每次加载完程序都需要重复以上步骤。

你可能感兴趣的:(STM32官方USB自定义HID类修改收发64字节)