Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262435
  • 博文数量: 65
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 375
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-29 21:49
个人简介

Flowers_World

文章分类

全部博文(65)

文章存档

2017年(7)

2016年(38)

2015年(14)

2014年(6)

我的朋友

分类: LINUX

2015-01-14 10:41:25

原文地址:STM32 USBHID应用 作者:sunhenu

    关于USB接口不多说了,你可以到网络上搜索很多的。我这里只是说说STM32的应用,具体USB的东西不多说。
    我在设计STM32程序的时候,如果需要和上位机通信,一般来说串口,485啊,或者CAN,也可以USB,或者网络通信,下面具体说说USB通信,正点原子的开发板提供了一些关于USB接口的使用例程,如果你将那个例程看懂的话,自己移植一个关于HID或者虚拟串口,甚至自己写一个固件,设计自己的驱动自己的VC应用都行,后者比较麻烦,对初学者估计很难。所以我就选择了移植HID,因为这个HID驱动在xp系统下是集成的,不需要我们去些驱动程序,我们只需要写好固件,和应用程序(VC6.0)就好了。
   下面就从固件程序开始说起,固件程序就是需要下载到STM32FLASH中的程序,我是开发环境是IAR5.30,因为中间涉及到很多东西,我简略说一下,USBSTM32库文件版本是3.3.0,其他外设STM32库文件时3.5.0.
USBHID组下的文件就是需要我们修改的文件,是针对HID的。
1,修改usb_desc.c文件,主要是设备描述符一些东西,网络上有相关例程可以参考

点击(此处)折叠或打开

  1. #include "usb_lib.h"
  2. #include "usb_desc.h"
  3. const u8 CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] =
  4.   {
  5.     0x12, /*bLength */
  6.     USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
  7.     0x00, /*bcdUSB */
  8.     0x02,
  9.     0x00, /*bDeviceClass*/
  10.     0x00, /*bDeviceSubClass*/
  11.     0x00, /*bDeviceProtocol*/
  12.     0x40, /*bMaxPacketSize40*/
  13.     0x83, /*idVendor (0x0483)*/
  14.     0x04,
  15.     0x50, /*idProduct = 0x5750*/
  16.     0x57,
  17.     0x00, /*bcdDevice rel. 2.00*/
  18.     0x02,
  19.     1, /*Index of string descriptor describing
  20.                                               manufacturer */
  21.     2, /*Index of string descriptor describing
  22.                                              product*/
  23.     3, /*Index of string descriptor describing the
  24.                                              device serial number */
  25.     0x01 /*bNumConfigurations*/
  26.   }
  27.   ; /* CustomHID_DeviceDescriptor */


  28. /* USB Configuration Descriptor */
  29. /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
  30. const u8 CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
  31.   {
  32.     0x09, /* bLength: Configuation Descriptor size */
  33.     USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
  34.     CUSTOMHID_SIZ_CONFIG_DESC,
  35.     /* wTotalLength: Bytes returned */
  36.     0x00,
  37.     0x01, /* bNumInterfaces: 1 interface */
  38.     0x01, /* bConfigurationValue: Configuration value */
  39.     0x00, /* iConfiguration: Index of string descriptor describing
  40.                                  the configuration*/
  41.     0xC0, /* bmAttributes: Bus powered */
  42.     0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */

  43.     /************** Descriptor of Custom HID interface ****************/
  44.     /* 09 */
  45.     0x09, /* bLength: Interface Descriptor size */
  46.     USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
  47.     0x00, /* bInterfaceNumber: Number of Interface */
  48.     0x00, /* bAlternateSetting: Alternate setting */
  49.     0x02, /* bNumEndpoints */
  50.     0x03, /* bInterfaceClass: HID */
  51.     0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  52.     0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  53.     0, /* iInterface: Index of string descriptor */
  54.     /******************** Descriptor of Custom HID HID ********************/
  55.     /* 18 */
  56.     0x09, /* bLength: HID Descriptor size */
  57.     HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
  58.     0x10, /* bcdHID: HID Class Spec release number */
  59.     0x01,
  60.     0x00, /* bCountryCode: Hardware target country */
  61.     0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  62.     0x22, /* bDescriptorType */
  63.     CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
  64.     0x00,
  65.     /******************** Descriptor of Custom HID endpoints ******************/
  66.     /* 27 */
  67.     0x07, /* bLength: Endpoint Descriptor size */
  68.     USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */

  69.     0x81, /* bEndpointAddress: Endpoint Address (IN) */
  70.     0x03, /* bmAttributes: Interrupt endpoint */
  71.     0x40, /* wMaxPacketSize: 64 Bytes max */
  72.     0x00,
  73.     0x20, /* bInterval: Polling Interval (32 ms) */
  74.     /* 34 */
  75.         
  76.     0x07,    /* bLength: Endpoint Descriptor size */
  77.     USB_ENDPOINT_DESCRIPTOR_TYPE,    /* bDescriptorType: */
  78.             /*    Endpoint descriptor type */
  79.     0x01,    /* bEndpointAddress: */
  80.             /*    Endpoint Address (OUT) */
  81.     0x03,    /* bmAttributes: Interrupt endpoint */
  82.     0x40,    /* wMaxPacketSize: 64 Bytes max */
  83.     0x00,
  84.     0x20,    /* bInterval: Polling Interval (20 ms) */
  85.     /* 41 */
  86.   }
  87.   ; /* CustomHID_ConfigDescriptor */
  88. const u8 CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
  89.   {
  90.     0x05, 0xFF, // USAGE_PAGE(User define)
  91.     0x09, 0xFF, // USAGE(User define)
  92.     0xa1, 0x01, // COLLECTION (Application)
  93.     0x05, 0x01, // USAGE_PAGE(1)
  94.     0x19, 0x00, // USAGE_MINIMUM(0)
  95.     0x29, 0xFF, // USAGE_MAXIMUM(255)
  96.     0x15, 0x00, // LOGICAL_MINIMUM (0)
  97.     0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  98.     0x75, 0x08, // REPORT_SIZE (8)
  99.     0x95, 0x40, // REPORT_COUNT (64)
  100.     0x81, 0x02, // INPUT (Data,Var,Abs)
  101.     0x05, 0x02, // USAGE_PAGE(2)
  102.     0x19, 0x00, // USAGE_MINIMUM (0)
  103.     0x29, 0xFF, // USAGE_MAXIMUM (255)
  104.     0x15, 0x00, // LOGICAL_MINIMUM (0)
  105.     0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  106.     0x95, 0x08, // REPORT_COUNT (8)
  107.     0x75, 0x40, // REPORT_SIZE (64)
  108.     0x91, 0x02, // OUTPUT (Data,Var,Abs)
  109.     0xc0 // END_COLLECTION
  110.   }; /* ReportDescriptor */

  111. /* USB String Descriptors (optional) */
  112. const u8 CustomHID_StringLangID[CUSTOMHID_SIZ_STRING_LANGID] =
  113.   {
  114.     CUSTOMHID_SIZ_STRING_LANGID,
  115.     USB_STRING_DESCRIPTOR_TYPE,
  116.     0x09,
  117.     0x04
  118.   }
  119.   ; /* LangID = 0x0409: U.S. English */

  120. const u8 CustomHID_StringVendor[CUSTOMHID_SIZ_STRING_VENDOR] =
  121.   {
  122.     CUSTOMHID_SIZ_STRING_VENDOR, /* Size of Vendor string */
  123.     USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType*/
  124.     0xd8, 0x98, //
  125.     0x6c, 0x62, //
  126.     0xe5, 0x5d, //¤
  127.     0x5c, 0x4f, //×÷
  128.     0xa4, 0x5b, //
  129.     0x68, 0x00, //h
  130.     0x74, 0x00, //t
  131.     0x74, 0x00, //t
  132.     0x70, 0x00, //p
  133.     0x3a, 0x00, //:
  134.     0x2f, 0x00, ///
  135.     0x2f, 0x00, ///
  136.     0x62, 0x00, //b
  137.     0x6c, 0x00, //l
  138.     0x6f, 0x00, //o
  139.     0x67, 0x00, //g
  140.     0x2e, 0x00, //.
  141.     0x63, 0x00, //c
  142.     0x73, 0x00, //s
  143.     0x64, 0x00, //d
  144.     0x6e, 0x00, //n
  145.     0x2e, 0x00, //.
  146.     0x6e, 0x00, //n
  147.     0x65, 0x00, //e
  148.     0x74, 0x00, //t
  149.     0x2f, 0x00, ///
  150.     0x63, 0x00, //c
  151.     0x79, 0x00, //y
  152.     0x37, 0x00, //7
  153.     0x35, 0x00, //5
  154.     0x37, 0x00, //7

  155.   };

  156. const u8 CustomHID_StringProduct[CUSTOMHID_SIZ_STRING_PRODUCT] =
  157.   {
  158.     CUSTOMHID_SIZ_STRING_PRODUCT, /* bLength */
  159.     USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
  160.     0xea, 0x81, //×
  161.     0x9a, 0x5b, //¨
  162.     0x49, 0x4e, //
  163.     0x84, 0x76, //
  164.     0x55, 0x00, //U
  165.     0x53, 0x00, //S
  166.     0x42, 0x00, //B
  167.     0x20, 0x00, //
  168.     0x48, 0x00, //H
  169.     0x49, 0x00, //I
  170.     0x44, 0x00, //D
  171.     0xbe, 0x8b, //è
  172.     0x07, 0x59, //±

  173.   };
  174. u8 CustomHID_StringSerial[CUSTOMHID_SIZ_STRING_SERIAL] =
  175.   {
  176.     CUSTOMHID_SIZ_STRING_SERIAL, /* bLength */
  177.     USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
  178.     'S', 0, 'T', 0, 'M', 0,'3', 0,'2', 0, '1', 0, '0', 0
  179.   };

  180. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

2,修改usb_prop.c文件,主要是CustomHID_Reset(void)函数,主要是设置端口0,端口1的发送地址和接收地址。

点击(此处)折叠或打开

  1. #include "usb_lib.h"
  2. #include "usb_conf.h"
  3. #include "usb_prop.h"
  4. #include "usb_desc.h"
  5. #include "usb_pwr.h"
  6. #include "usb_hw.h"

  7. //static uint8_t s_Request = 0;

  8. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  9. * File Name : usb_prop.c
  10. * Author : MCD Application Team
  11. * Version : V2.2.0
  12. * Date : 06/13/2008
  13. * Description : All processings related to Custom HID Demo
  14. ********************************************************************************
  15. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  16. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  17. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  19. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  20. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  21. *******************************************************************************/

  22. /* Includes ------------------------------------------------------------------*/






  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. u32 ProtocolValue;

  28. /* -------------------------------------------------------------------------- */
  29. /* Structures initializations */
  30. /* -------------------------------------------------------------------------- */

  31. DEVICE Device_Table =
  32.   {
  33.     EP_NUM,
  34.     1
  35.   };

  36. DEVICE_PROP Device_Property =
  37.   {
  38.     CustomHID_init,
  39.     CustomHID_Reset,
  40.     CustomHID_Status_In,
  41.     CustomHID_Status_Out,
  42.     CustomHID_Data_Setup,
  43.     CustomHID_NoData_Setup,
  44.     CustomHID_Get_Interface_Setting,
  45.     CustomHID_GetDeviceDescriptor,
  46.     CustomHID_GetConfigDescriptor,
  47.     CustomHID_GetStringDescriptor,
  48.     0,
  49.     0x40 /*MAX PACKET SIZE*/
  50.   };
  51. USER_STANDARD_REQUESTS User_Standard_Requests =
  52.   {
  53.     CustomHID_GetConfiguration,
  54.     CustomHID_SetConfiguration,
  55.     CustomHID_GetInterface,
  56.     CustomHID_SetInterface,
  57.     CustomHID_GetStatus,
  58.     CustomHID_ClearFeature,
  59.     CustomHID_SetEndPointFeature,
  60.     CustomHID_SetDeviceFeature,
  61.     CustomHID_SetDeviceAddress
  62.   };

  63. ONE_DESCRIPTOR Device_Descriptor =
  64.   {
  65.     (u8*)CustomHID_DeviceDescriptor,
  66.     CUSTOMHID_SIZ_DEVICE_DESC
  67.   };

  68. ONE_DESCRIPTOR Config_Descriptor =
  69.   {
  70.     (u8*)CustomHID_ConfigDescriptor,
  71.     CUSTOMHID_SIZ_CONFIG_DESC
  72.   };

  73. ONE_DESCRIPTOR CustomHID_Report_Descriptor =
  74.   {
  75.     (u8 *)CustomHID_ReportDescriptor,
  76.     CUSTOMHID_SIZ_REPORT_DESC
  77.   };

  78. ONE_DESCRIPTOR CustomHID_Hid_Descriptor =
  79.   {
  80.     (u8*)CustomHID_ConfigDescriptor + CUSTOMHID_OFF_HID_DESC,
  81.     CUSTOMHID_SIZ_HID_DESC
  82.   };

  83. ONE_DESCRIPTOR String_Descriptor[4] =
  84.   {
  85.     {(u8*)CustomHID_StringLangID, CUSTOMHID_SIZ_STRING_LANGID},
  86.     {(u8*)CustomHID_StringVendor, CUSTOMHID_SIZ_STRING_VENDOR},
  87.     {(u8*)CustomHID_StringProduct, CUSTOMHID_SIZ_STRING_PRODUCT},
  88.     {(u8*)CustomHID_StringSerial, CUSTOMHID_SIZ_STRING_SERIAL}
  89.   };

  90. /* Extern variables ----------------------------------------------------------*/
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Extern function prototypes ------------------------------------------------*/
  93. /* Private functions ---------------------------------------------------------*/

  94. /*******************************************************************************
  95. * Function Name : CustomHID_init.
  96. * Description : Custom HID init routine.
  97. * Input : None.
  98. * Output : None.
  99. * Return : None.
  100. *******************************************************************************/
  101. void CustomHID_init(void)
  102. {
  103.   /* Update the serial number string descriptor with the data from the unique
  104.   ID*/
  105. // Get_SerialNum();
  106.     
  107.   pInformation->Current_Configuration = 0;
  108.   /* Connect the device */
  109.   //PowerOn();
  110.   usb_PowerOn();
  111.   /* USB interrupts initialization */
  112.   _SetISTR(0); /* clear pending interrupts */
  113.   wInterrupt_Mask = IMR_MSK;
  114.   _SetCNTR(wInterrupt_Mask); /* set interrupts mask */

  115.   bDeviceState = UNCONNECTED;
  116. }

  117. /*******************************************************************************
  118. * Function Name : CustomHID_Reset.
  119. * Description : Custom HID reset routine.
  120. * Input : None.
  121. * Output : None.
  122. * Return : None.
  123. *******************************************************************************/
  124. void CustomHID_Reset(void)
  125. {
  126.   /* Set Joystick_DEVICE as not configured */
  127.   pInformation->Current_Configuration = 0;
  128.   pInformation->Current_Interface = 0;/*the default Interface*/
  129.   
  130.   pInformation->Current_Feature = CustomHID_ConfigDescriptor[7];

  131.    SetBTABLE(BTABLE_ADDRESS);
  132.   /* Initialize Endpoint 0 */
  133.   SetEPType(ENDP0, EP_CONTROL);
  134.   SetEPTxStatus(ENDP0, EP_TX_STALL);
  135.   SetEPRxAddr(ENDP0, ENDP0_RXADDR);
  136.   SetEPTxAddr(ENDP0, ENDP0_TXADDR);
  137.   Clear_Status_Out(ENDP0);
  138.   SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
  139.   SetEPRxValid(ENDP0);

  140.   /* Initialize Endpoint 1 */
  141.   SetEPType(ENDP1, EP_INTERRUPT);
  142.   SetEPTxAddr(ENDP1, ENDP1_TXADDR);
  143.   SetEPTxCount(ENDP1, 64);
  144.   SetEPRxStatus(ENDP1, EP_RX_DIS);
  145.   SetEPTxStatus(ENDP1, EP_TX_NAK);

  146.   /* Initialize Endpoint 1 */
  147.  // SetEPType(ENDP1, EP_INTERRUPT);
  148.   SetEPRxAddr(ENDP1, ENDP1_RXADDR);
  149.   SetEPRxCount(ENDP1, 64);
  150.  // SetEPTxStatus(ENDP1, EP_TX_DIS);
  151.   SetEPRxStatus(ENDP1, EP_RX_VALID);
  152.    bDeviceState = ATTACHED;
  153.   /* Set this device to response on default address */
  154.   SetDeviceAddress(0);
  155. }
  156. /*******************************************************************************
  157. * Function Name : CustomHID_SetConfiguration.
  158. * Description : Udpade the device state to configured and command the ADC
  159. * conversion.
  160. * Input : None.
  161. * Output : None.
  162. * Return : None.
  163. *******************************************************************************/
  164. void CustomHID_SetConfiguration(void)
  165. {
  166.   if (pInformation->Current_Configuration != 0)
  167.   {
  168.     /* Device configured */
  169.     bDeviceState = CONFIGURED;
  170.     
  171.     /* Start ADC1 Software Conversion */
  172.     ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  173.   }
  174. }
  175. /*******************************************************************************
  176. * Function Name : CustomHID_SetConfiguration.
  177. * Description : Udpade the device state to addressed.
  178. * Input : None.
  179. * Output : None.
  180. * Return : None.
  181. *******************************************************************************/
  182. void CustomHID_SetDeviceAddress (void)
  183. {
  184.   bDeviceState = ADDRESSED;
  185. }
  186. /*******************************************************************************
  187. * Function Name : CustomHID_Status_In.
  188. * Description : Joystick status IN routine.
  189. * Input : None.
  190. * Output : None.
  191. * Return : None.
  192. *******************************************************************************/
  193. void CustomHID_Status_In(void)
  194. {
  195. }

  196. /*******************************************************************************
  197. * Function Name : CustomHID_Status_Out
  198. * Description : Joystick status OUT routine.
  199. * Input : None.
  200. * Output : None.
  201. * Return : None.
  202. *******************************************************************************/
  203. void CustomHID_Status_Out (void)
  204. {
  205. }

  206. /*******************************************************************************
  207. * Function Name : CustomHID_Data_Setup
  208. * Description : Handle the data class specific requests.
  209. * Input : Request Nb.
  210. * Output : None.
  211. * Return : USB_UNSUPPORT or USB_SUCCESS.
  212. *******************************************************************************/
  213. RESULT CustomHID_Data_Setup(u8 RequestNo)
  214. {
  215.   u8 *(*CopyRoutine)(u16);

  216.   CopyRoutine = NULL;

  217.   if ((RequestNo == GET_DESCRIPTOR)
  218.       && (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
  219.       && (pInformation->USBwIndex0 == 0))
  220.   {

  221.     if (pInformation->USBwValue1 == REPORT_DESCRIPTOR)
  222.     {
  223.       CopyRoutine = CustomHID_GetReportDescriptor;
  224.     }
  225.     else if (pInformation->USBwValue1 == HID_DESCRIPTOR_TYPE)
  226.     {
  227.       CopyRoutine = CustomHID_GetHIDDescriptor;
  228.     }

  229.   } /* End of GET_DESCRIPTOR */

  230.   /*** GET_PROTOCOL ***/
  231.   else if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
  232.            && RequestNo == GET_PROTOCOL)
  233.   {
  234.     CopyRoutine = CustomHID_GetProtocolValue;
  235.   }

  236.   if (CopyRoutine == NULL)
  237.   {
  238.     return USB_UNSUPPORT;
  239.   }

  240.   pInformation->Ctrl_Info.CopyData = CopyRoutine;
  241.   pInformation->Ctrl_Info.Usb_wOffset = 0;
  242.   (*CopyRoutine)(0);
  243.   return USB_SUCCESS;
  244. }

  245. /*******************************************************************************
  246. * Function Name : CustomHID_NoData_Setup
  247. * Description : handle the no data class specific requests
  248. * Input : Request Nb.
  249. * Output : None.
  250. * Return : USB_UNSUPPORT or USB_SUCCESS.
  251. *******************************************************************************/
  252. RESULT CustomHID_NoData_Setup(u8 RequestNo)
  253. {
  254.   if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
  255.       && (RequestNo == SET_PROTOCOL))
  256.   {
  257.     return CustomHID_SetProtocol();
  258.   }

  259.   else
  260.   {
  261.     return USB_UNSUPPORT;
  262.   }
  263. }

  264. /*******************************************************************************
  265. * Function Name : CustomHID_GetDeviceDescriptor.
  266. * Description : Gets the device descriptor.
  267. * Input : Length
  268. * Output : None.
  269. * Return : The address of the device descriptor.
  270. *******************************************************************************/
  271. u8 *CustomHID_GetDeviceDescriptor(u16 Length)
  272. {
  273.   return Standard_GetDescriptorData(Length, &Device_Descriptor);
  274. }

  275. /*******************************************************************************
  276. * Function Name : CustomHID_GetConfigDescriptor.
  277. * Description : Gets the configuration descriptor.
  278. * Input : Length
  279. * Output : None.
  280. * Return : The address of the configuration descriptor.
  281. *******************************************************************************/
  282. u8 *CustomHID_GetConfigDescriptor(u16 Length)
  283. {
  284.   return Standard_GetDescriptorData(Length, &Config_Descriptor);
  285. }

  286. /*******************************************************************************
  287. * Function Name : CustomHID_GetStringDescriptor
  288. * Description : Gets the string descriptors according to the needed index
  289. * Input : Length
  290. * Output : None.
  291. * Return : The address of the string descriptors.
  292. *******************************************************************************/
  293. u8 *CustomHID_GetStringDescriptor(u16 Length)
  294. {
  295.   u8 wValue0 = pInformation->USBwValue0;
  296.   if (wValue0 > 4)
  297.   {
  298.     return NULL;
  299.   }
  300.   else
  301.   {
  302.     return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
  303.   }
  304. }

  305. /*******************************************************************************
  306. * Function Name : CustomHID_GetReportDescriptor.
  307. * Description : Gets the HID report descriptor.
  308. * Input : Length
  309. * Output : None.
  310. * Return : The address of the configuration descriptor.
  311. *******************************************************************************/
  312. u8 *CustomHID_GetReportDescriptor(u16 Length)
  313. {
  314.   return Standard_GetDescriptorData(Length, &CustomHID_Report_Descriptor);
  315. }

  316. /*******************************************************************************
  317. * Function Name : CustomHID_GetHIDDescriptor.
  318. * Description : Gets the HID descriptor.
  319. * Input : Length
  320. * Output : None.
  321. * Return : The address of the configuration descriptor.
  322. *******************************************************************************/
  323. u8 *CustomHID_GetHIDDescriptor(u16 Length)
  324. {
  325.   return Standard_GetDescriptorData(Length, &CustomHID_Hid_Descriptor);
  326. }

  327. /*******************************************************************************
  328. * Function Name : CustomHID_Get_Interface_Setting.
  329. * Description : tests the interface and the alternate setting according to the
  330. * supported one.
  331. * Input : - Interface : interface number.
  332. * - AlternateSetting : Alternate Setting number.
  333. * Output : None.
  334. * Return : USB_SUCCESS or USB_UNSUPPORT.
  335. *******************************************************************************/
  336. RESULT CustomHID_Get_Interface_Setting(u8 Interface, u8 AlternateSetting)
  337. {
  338.   if (AlternateSetting > 0)
  339.   {
  340.     return USB_UNSUPPORT;
  341.   }
  342.   else if (Interface > 0)
  343.   {
  344.     return USB_UNSUPPORT;
  345.   }
  346.   return USB_SUCCESS;
  347. }

  348. /*******************************************************************************
  349. * Function Name : CustomHID_SetProtocol
  350. * Description : Joystick Set Protocol request routine.
  351. * Input : None.
  352. * Output : None.
  353. * Return : USB SUCCESS.
  354. *******************************************************************************/
  355. RESULT CustomHID_SetProtocol(void)
  356. {
  357.   u8 wValue0 = pInformation->USBwValue0;
  358.   ProtocolValue = wValue0;
  359.   return USB_SUCCESS;
  360. }

  361. /*******************************************************************************
  362. * Function Name : CustomHID_GetProtocolValue
  363. * Description : get the protocol value
  364. * Input : Length.
  365. * Output : None.
  366. * Return : address of the protcol value.
  367. *******************************************************************************/
  368. u8 *CustomHID_GetProtocolValue(u16 Length)
  369. {
  370.   if (Length == 0)
  371.   {
  372.     pInformation->Ctrl_Info.Usb_wLength = 1;
  373.     return NULL;
  374.   }
  375.   else
  376.   {
  377.     return (u8 *)(&ProtocolValue);
  378.   }
  379. }

  380. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

3,修改usb_endp.c文件,这是端口接收数据函数。

点击(此处)折叠或打开

  1. #include "usb_lib.h"
  2. #include "usb_desc.h"
  3. #include "usb_mem.h"
  4. #include "usb_hw.h"
  5. #include "usb_istr.h"
  6. #include "usb_pwr.h"

  7. /* Interval between sending IN packets in frame number (1 frame = 1ms) */
  8. /*#define VCOMPORT_IN_FRAME_INTERVAL 5

  9. extern uint8_t USART_Rx_Buffer[];
  10. extern uint32_t USART_Rx_ptr_out;
  11. extern uint32_t USART_Rx_length;
  12. extern uint8_t USB_Tx_State;*/
  13. u8 Receive_Buffer[64];
  14. /*
  15. *********************************************************************************************************
  16. *     : EP1_IN_Callback
  17. *    ÷: 1 IN°ü¨è±->PC÷
  18. *     :
  19. *    · :
  20. *********************************************************************************************************
  21. u8 Receive_Buffer[64];

  22. /* Private function prototypes -----------------------------------------------*/
  23. /* Private functions ---------------------------------------------------------*/
  24. /*******************************************************************************
  25. * Function Name : EP1_OUT_Callback.
  26. * Description : EP1 OUT Callback Routine.
  27. * Input : None.
  28. * Output : None.
  29. * Return : None.
  30. *******************************************************************************/
  31. void EP1_OUT_Callback(void)
  32. {
  33.     /*u8 DataLen;
  34.     DataLen = GetEPRxCount(ENDP1);
  35.     PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, DataLen);
  36.     SetEPRxValid(ENDP1);*/
  37.   u8 DataLen;
  38.   DataLen = GetEPRxCount(ENDP1);
  39.   PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, DataLen);
  40.   SetEPRxValid(ENDP1);
  41.     
  42.   UserToPMABufferCopy(Receive_Buffer, GetEPTxAddr(ENDP1), DataLen);
  43.   SetEPTxCount(ENDP1, DataLen);
  44.   SetEPTxValid(ENDP1);
  45.  
  46. }
  47. /*
  48. *********************************************************************************************************
  49. *     : EP3_OUT_Callback
  50. *    ÷: 3 OUT°ü¨PC->è±÷
  51. *     :
  52. *    · :
  53. *********************************************************************************************************
  54. */
  55. /*void EP3_OUT_Callback(void)
  56. {
  57.     uint16_t usRxCnt;
  58.     //uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
  59.     
  60.     /* USB3USB_Rx_Buffer ó±USB_Rx_Cnt */
  61.     //usRxCnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);
  62.     
  63.     /* */
  64.     //usb_SaveHostDataToBuf(USB_Rx_Buffer, usRxCnt);
  65.     
  66.     /* í EP3 */
  67.     //SetEPRxValid(ENDP3);
  68. //}

  69. /*
  70. *********************************************************************************************************
  71. *     : SOF_Callback
  72. *    ÷: SOF÷ .SOFhostframe·
  73. *     :
  74. *    · :
  75. *********************************************************************************************************
  76. */
  77. /*void SOF_Callback(void)
  78. {
  79.     static uint32_t FrameCount = 0;
  80.     
  81.     if (bDeviceState == CONFIGURED)
  82.     {
  83.         if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
  84.         {
  85.             /* Reset the frame counter */
  86.             //FrameCount = 0;
  87.             
  88.             /* Check the data to be sent through IN pipe */
  89.             //EP1_IN_Callback();
  90.             //Handle_USBAsynchXfer();
  91.         //}
  92.     //}
  93. //}

   修改这个函数之后,编译时会提示重定义NOP_Process,这个问题的解决办法是:在usb_config.h中找到这样一句:
#define  EP1_OUT_Callback  NOP_Process,
将这一句注释掉,使得EP1接受函数不是空进程了,程序就运行成功了。
   还有就是我在这里遇见了no define ADC_SoftStartConvertCmd()这样的错误,你看看你的ST库函数
里面将ADC的库添加没有,添加以后,错误消失。
   接下来就是应用程序了,我利用了百合电子工作室的VC6.0例程,修改了一下,VID和PID,然后添加几个按钮来控制发送数据。
驱动程序加载成功,
发送数据到EP1,然后STM32利用EP1将数据返回。
我将从USB接受到的64字节数据在LCD显示出来了,看图片,那个绿色的LED就是上面软件LEDON和LEDoff按钮控制的。
下面给2个链接,可以参考一下,我就是参考他们的代码移植成功的。在此也表示感谢。
附件是hex文件,在正点原子战舰开发板验证过,没有问题。bushound50是调试USB常用的软件。
http://blog.csdn.net/taot2009/article/details/7363091 
http://www.baiheee.com/OpenSource/Easy%20USB%2051%20Programer/Easy%20USB%2051%20Programer9.htm


阅读(1373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~