关于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文件,主要是设备描述符一些东西,网络上有相关例程可以参考
- #include "usb_lib.h"
- #include "usb_desc.h"
- const u8 CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] =
- {
- 0x12, /*bLength */
- USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
- 0x00, /*bcdUSB */
- 0x02,
- 0x00, /*bDeviceClass*/
- 0x00, /*bDeviceSubClass*/
- 0x00, /*bDeviceProtocol*/
- 0x40, /*bMaxPacketSize40*/
- 0x83, /*idVendor (0x0483)*/
- 0x04,
- 0x50, /*idProduct = 0x5750*/
- 0x57,
- 0x00, /*bcdDevice rel. 2.00*/
- 0x02,
- 1, /*Index of string descriptor describing
- manufacturer */
- 2, /*Index of string descriptor describing
- product*/
- 3, /*Index of string descriptor describing the
- device serial number */
- 0x01 /*bNumConfigurations*/
- }
- ; /* CustomHID_DeviceDescriptor */
- /* USB Configuration Descriptor */
- /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
- const u8 CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
- {
- 0x09, /* bLength: Configuation Descriptor size */
- USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
- CUSTOMHID_SIZ_CONFIG_DESC,
- /* wTotalLength: Bytes returned */
- 0x00,
- 0x01, /* bNumInterfaces: 1 interface */
- 0x01, /* bConfigurationValue: Configuration value */
- 0x00, /* iConfiguration: Index of string descriptor describing
- the configuration*/
- 0xC0, /* bmAttributes: Bus powered */
- 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */
- /************** Descriptor of Custom HID interface ****************/
- /* 09 */
- 0x09, /* bLength: Interface Descriptor size */
- USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
- 0x00, /* bInterfaceNumber: Number of Interface */
- 0x00, /* bAlternateSetting: Alternate setting */
- 0x02, /* bNumEndpoints */
- 0x03, /* bInterfaceClass: HID */
- 0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
- 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
- 0, /* iInterface: Index of string descriptor */
- /******************** Descriptor of Custom HID HID ********************/
- /* 18 */
- 0x09, /* bLength: HID Descriptor size */
- HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
- 0x10, /* bcdHID: HID Class Spec release number */
- 0x01,
- 0x00, /* bCountryCode: Hardware target country */
- 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
- 0x22, /* bDescriptorType */
- CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
- 0x00,
- /******************** Descriptor of Custom HID endpoints ******************/
- /* 27 */
- 0x07, /* bLength: Endpoint Descriptor size */
- USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
- 0x81, /* bEndpointAddress: Endpoint Address (IN) */
- 0x03, /* bmAttributes: Interrupt endpoint */
- 0x40, /* wMaxPacketSize: 64 Bytes max */
- 0x00,
- 0x20, /* bInterval: Polling Interval (32 ms) */
- /* 34 */
-
- 0x07, /* bLength: Endpoint Descriptor size */
- USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
- /* Endpoint descriptor type */
- 0x01, /* bEndpointAddress: */
- /* Endpoint Address (OUT) */
- 0x03, /* bmAttributes: Interrupt endpoint */
- 0x40, /* wMaxPacketSize: 64 Bytes max */
- 0x00,
- 0x20, /* bInterval: Polling Interval (20 ms) */
- /* 41 */
- }
- ; /* CustomHID_ConfigDescriptor */
- const u8 CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
- {
- 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, 0x40, // REPORT_SIZE (64)
- 0x91, 0x02, // OUTPUT (Data,Var,Abs)
- 0xc0 // END_COLLECTION
- }; /* ReportDescriptor */
- /* USB String Descriptors (optional) */
- const u8 CustomHID_StringLangID[CUSTOMHID_SIZ_STRING_LANGID] =
- {
- CUSTOMHID_SIZ_STRING_LANGID,
- USB_STRING_DESCRIPTOR_TYPE,
- 0x09,
- 0x04
- }
- ; /* LangID = 0x0409: U.S. English */
- const u8 CustomHID_StringVendor[CUSTOMHID_SIZ_STRING_VENDOR] =
- {
- CUSTOMHID_SIZ_STRING_VENDOR, /* Size of Vendor string */
- USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType*/
- 0xd8, 0x98, //
- 0x6c, 0x62, //
- 0xe5, 0x5d, //¤
- 0x5c, 0x4f, //×÷
- 0xa4, 0x5b, //
- 0x68, 0x00, //h
- 0x74, 0x00, //t
- 0x74, 0x00, //t
- 0x70, 0x00, //p
- 0x3a, 0x00, //:
- 0x2f, 0x00, ///
- 0x2f, 0x00, ///
- 0x62, 0x00, //b
- 0x6c, 0x00, //l
- 0x6f, 0x00, //o
- 0x67, 0x00, //g
- 0x2e, 0x00, //.
- 0x63, 0x00, //c
- 0x73, 0x00, //s
- 0x64, 0x00, //d
- 0x6e, 0x00, //n
- 0x2e, 0x00, //.
- 0x6e, 0x00, //n
- 0x65, 0x00, //e
- 0x74, 0x00, //t
- 0x2f, 0x00, ///
- 0x63, 0x00, //c
- 0x79, 0x00, //y
- 0x37, 0x00, //7
- 0x35, 0x00, //5
- 0x37, 0x00, //7
- };
- const u8 CustomHID_StringProduct[CUSTOMHID_SIZ_STRING_PRODUCT] =
- {
- CUSTOMHID_SIZ_STRING_PRODUCT, /* bLength */
- USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
- 0xea, 0x81, //×
- 0x9a, 0x5b, //¨
- 0x49, 0x4e, //
- 0x84, 0x76, //
- 0x55, 0x00, //U
- 0x53, 0x00, //S
- 0x42, 0x00, //B
- 0x20, 0x00, //
- 0x48, 0x00, //H
- 0x49, 0x00, //I
- 0x44, 0x00, //D
- 0xbe, 0x8b, //è
- 0x07, 0x59, //±
- };
- u8 CustomHID_StringSerial[CUSTOMHID_SIZ_STRING_SERIAL] =
- {
- CUSTOMHID_SIZ_STRING_SERIAL, /* bLength */
- USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
- 'S', 0, 'T', 0, 'M', 0,'3', 0,'2', 0, '1', 0, '0', 0
- };
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
2,修改usb_prop.c文件,主要是CustomHID_Reset(void)函数,主要是设置端口0,端口1的发送地址和接收地址。
- #include "usb_lib.h"
- #include "usb_conf.h"
- #include "usb_prop.h"
- #include "usb_desc.h"
- #include "usb_pwr.h"
- #include "usb_hw.h"
- //static uint8_t s_Request = 0;
- /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
- * File Name : usb_prop.c
- * Author : MCD Application Team
- * Version : V2.2.0
- * Date : 06/13/2008
- * Description : All processings related to Custom HID Demo
- ********************************************************************************
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
- * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
- * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
- * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
- * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- u32 ProtocolValue;
- /* -------------------------------------------------------------------------- */
- /* Structures initializations */
- /* -------------------------------------------------------------------------- */
- DEVICE Device_Table =
- {
- EP_NUM,
- 1
- };
- DEVICE_PROP Device_Property =
- {
- CustomHID_init,
- CustomHID_Reset,
- CustomHID_Status_In,
- CustomHID_Status_Out,
- CustomHID_Data_Setup,
- CustomHID_NoData_Setup,
- CustomHID_Get_Interface_Setting,
- CustomHID_GetDeviceDescriptor,
- CustomHID_GetConfigDescriptor,
- CustomHID_GetStringDescriptor,
- 0,
- 0x40 /*MAX PACKET SIZE*/
- };
- USER_STANDARD_REQUESTS User_Standard_Requests =
- {
- CustomHID_GetConfiguration,
- CustomHID_SetConfiguration,
- CustomHID_GetInterface,
- CustomHID_SetInterface,
- CustomHID_GetStatus,
- CustomHID_ClearFeature,
- CustomHID_SetEndPointFeature,
- CustomHID_SetDeviceFeature,
- CustomHID_SetDeviceAddress
- };
- ONE_DESCRIPTOR Device_Descriptor =
- {
- (u8*)CustomHID_DeviceDescriptor,
- CUSTOMHID_SIZ_DEVICE_DESC
- };
- ONE_DESCRIPTOR Config_Descriptor =
- {
- (u8*)CustomHID_ConfigDescriptor,
- CUSTOMHID_SIZ_CONFIG_DESC
- };
- ONE_DESCRIPTOR CustomHID_Report_Descriptor =
- {
- (u8 *)CustomHID_ReportDescriptor,
- CUSTOMHID_SIZ_REPORT_DESC
- };
- ONE_DESCRIPTOR CustomHID_Hid_Descriptor =
- {
- (u8*)CustomHID_ConfigDescriptor + CUSTOMHID_OFF_HID_DESC,
- CUSTOMHID_SIZ_HID_DESC
- };
- ONE_DESCRIPTOR String_Descriptor[4] =
- {
- {(u8*)CustomHID_StringLangID, CUSTOMHID_SIZ_STRING_LANGID},
- {(u8*)CustomHID_StringVendor, CUSTOMHID_SIZ_STRING_VENDOR},
- {(u8*)CustomHID_StringProduct, CUSTOMHID_SIZ_STRING_PRODUCT},
- {(u8*)CustomHID_StringSerial, CUSTOMHID_SIZ_STRING_SERIAL}
- };
- /* Extern variables ----------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Extern function prototypes ------------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /*******************************************************************************
- * Function Name : CustomHID_init.
- * Description : Custom HID init routine.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_init(void)
- {
- /* Update the serial number string descriptor with the data from the unique
- ID*/
- // Get_SerialNum();
-
- pInformation->Current_Configuration = 0;
- /* Connect the device */
- //PowerOn();
- usb_PowerOn();
- /* USB interrupts initialization */
- _SetISTR(0); /* clear pending interrupts */
- wInterrupt_Mask = IMR_MSK;
- _SetCNTR(wInterrupt_Mask); /* set interrupts mask */
- bDeviceState = UNCONNECTED;
- }
- /*******************************************************************************
- * Function Name : CustomHID_Reset.
- * Description : Custom HID reset routine.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_Reset(void)
- {
- /* Set Joystick_DEVICE as not configured */
- pInformation->Current_Configuration = 0;
- pInformation->Current_Interface = 0;/*the default Interface*/
-
- pInformation->Current_Feature = CustomHID_ConfigDescriptor[7];
- SetBTABLE(BTABLE_ADDRESS);
- /* Initialize Endpoint 0 */
- SetEPType(ENDP0, EP_CONTROL);
- SetEPTxStatus(ENDP0, EP_TX_STALL);
- SetEPRxAddr(ENDP0, ENDP0_RXADDR);
- SetEPTxAddr(ENDP0, ENDP0_TXADDR);
- Clear_Status_Out(ENDP0);
- SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
- SetEPRxValid(ENDP0);
- /* Initialize Endpoint 1 */
- SetEPType(ENDP1, EP_INTERRUPT);
- SetEPTxAddr(ENDP1, ENDP1_TXADDR);
- SetEPTxCount(ENDP1, 64);
- SetEPRxStatus(ENDP1, EP_RX_DIS);
- SetEPTxStatus(ENDP1, EP_TX_NAK);
- /* Initialize Endpoint 1 */
- // SetEPType(ENDP1, EP_INTERRUPT);
- SetEPRxAddr(ENDP1, ENDP1_RXADDR);
- SetEPRxCount(ENDP1, 64);
- // SetEPTxStatus(ENDP1, EP_TX_DIS);
- SetEPRxStatus(ENDP1, EP_RX_VALID);
- bDeviceState = ATTACHED;
- /* Set this device to response on default address */
- SetDeviceAddress(0);
- }
- /*******************************************************************************
- * Function Name : CustomHID_SetConfiguration.
- * Description : Udpade the device state to configured and command the ADC
- * conversion.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_SetConfiguration(void)
- {
- if (pInformation->Current_Configuration != 0)
- {
- /* Device configured */
- bDeviceState = CONFIGURED;
-
- /* Start ADC1 Software Conversion */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- }
- /*******************************************************************************
- * Function Name : CustomHID_SetConfiguration.
- * Description : Udpade the device state to addressed.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_SetDeviceAddress (void)
- {
- bDeviceState = ADDRESSED;
- }
- /*******************************************************************************
- * Function Name : CustomHID_Status_In.
- * Description : Joystick status IN routine.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_Status_In(void)
- {
- }
- /*******************************************************************************
- * Function Name : CustomHID_Status_Out
- * Description : Joystick status OUT routine.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void CustomHID_Status_Out (void)
- {
- }
- /*******************************************************************************
- * Function Name : CustomHID_Data_Setup
- * Description : Handle the data class specific requests.
- * Input : Request Nb.
- * Output : None.
- * Return : USB_UNSUPPORT or USB_SUCCESS.
- *******************************************************************************/
- RESULT CustomHID_Data_Setup(u8 RequestNo)
- {
- u8 *(*CopyRoutine)(u16);
- CopyRoutine = NULL;
- if ((RequestNo == GET_DESCRIPTOR)
- && (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
- && (pInformation->USBwIndex0 == 0))
- {
- if (pInformation->USBwValue1 == REPORT_DESCRIPTOR)
- {
- CopyRoutine = CustomHID_GetReportDescriptor;
- }
- else if (pInformation->USBwValue1 == HID_DESCRIPTOR_TYPE)
- {
- CopyRoutine = CustomHID_GetHIDDescriptor;
- }
- } /* End of GET_DESCRIPTOR */
- /*** GET_PROTOCOL ***/
- else if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
- && RequestNo == GET_PROTOCOL)
- {
- CopyRoutine = CustomHID_GetProtocolValue;
- }
- if (CopyRoutine == NULL)
- {
- return USB_UNSUPPORT;
- }
- pInformation->Ctrl_Info.CopyData = CopyRoutine;
- pInformation->Ctrl_Info.Usb_wOffset = 0;
- (*CopyRoutine)(0);
- return USB_SUCCESS;
- }
- /*******************************************************************************
- * Function Name : CustomHID_NoData_Setup
- * Description : handle the no data class specific requests
- * Input : Request Nb.
- * Output : None.
- * Return : USB_UNSUPPORT or USB_SUCCESS.
- *******************************************************************************/
- RESULT CustomHID_NoData_Setup(u8 RequestNo)
- {
- if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
- && (RequestNo == SET_PROTOCOL))
- {
- return CustomHID_SetProtocol();
- }
- else
- {
- return USB_UNSUPPORT;
- }
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetDeviceDescriptor.
- * Description : Gets the device descriptor.
- * Input : Length
- * Output : None.
- * Return : The address of the device descriptor.
- *******************************************************************************/
- u8 *CustomHID_GetDeviceDescriptor(u16 Length)
- {
- return Standard_GetDescriptorData(Length, &Device_Descriptor);
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetConfigDescriptor.
- * Description : Gets the configuration descriptor.
- * Input : Length
- * Output : None.
- * Return : The address of the configuration descriptor.
- *******************************************************************************/
- u8 *CustomHID_GetConfigDescriptor(u16 Length)
- {
- return Standard_GetDescriptorData(Length, &Config_Descriptor);
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetStringDescriptor
- * Description : Gets the string descriptors according to the needed index
- * Input : Length
- * Output : None.
- * Return : The address of the string descriptors.
- *******************************************************************************/
- u8 *CustomHID_GetStringDescriptor(u16 Length)
- {
- u8 wValue0 = pInformation->USBwValue0;
- if (wValue0 > 4)
- {
- return NULL;
- }
- else
- {
- return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
- }
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetReportDescriptor.
- * Description : Gets the HID report descriptor.
- * Input : Length
- * Output : None.
- * Return : The address of the configuration descriptor.
- *******************************************************************************/
- u8 *CustomHID_GetReportDescriptor(u16 Length)
- {
- return Standard_GetDescriptorData(Length, &CustomHID_Report_Descriptor);
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetHIDDescriptor.
- * Description : Gets the HID descriptor.
- * Input : Length
- * Output : None.
- * Return : The address of the configuration descriptor.
- *******************************************************************************/
- u8 *CustomHID_GetHIDDescriptor(u16 Length)
- {
- return Standard_GetDescriptorData(Length, &CustomHID_Hid_Descriptor);
- }
- /*******************************************************************************
- * Function Name : CustomHID_Get_Interface_Setting.
- * Description : tests the interface and the alternate setting according to the
- * supported one.
- * Input : - Interface : interface number.
- * - AlternateSetting : Alternate Setting number.
- * Output : None.
- * Return : USB_SUCCESS or USB_UNSUPPORT.
- *******************************************************************************/
- RESULT CustomHID_Get_Interface_Setting(u8 Interface, u8 AlternateSetting)
- {
- if (AlternateSetting > 0)
- {
- return USB_UNSUPPORT;
- }
- else if (Interface > 0)
- {
- return USB_UNSUPPORT;
- }
- return USB_SUCCESS;
- }
- /*******************************************************************************
- * Function Name : CustomHID_SetProtocol
- * Description : Joystick Set Protocol request routine.
- * Input : None.
- * Output : None.
- * Return : USB SUCCESS.
- *******************************************************************************/
- RESULT CustomHID_SetProtocol(void)
- {
- u8 wValue0 = pInformation->USBwValue0;
- ProtocolValue = wValue0;
- return USB_SUCCESS;
- }
- /*******************************************************************************
- * Function Name : CustomHID_GetProtocolValue
- * Description : get the protocol value
- * Input : Length.
- * Output : None.
- * Return : address of the protcol value.
- *******************************************************************************/
- u8 *CustomHID_GetProtocolValue(u16 Length)
- {
- if (Length == 0)
- {
- pInformation->Ctrl_Info.Usb_wLength = 1;
- return NULL;
- }
- else
- {
- return (u8 *)(&ProtocolValue);
- }
- }
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
3,修改usb_endp.c文件,这是端口接收数据函数。
- #include "usb_lib.h"
- #include "usb_desc.h"
- #include "usb_mem.h"
- #include "usb_hw.h"
- #include "usb_istr.h"
- #include "usb_pwr.h"
- /* Interval between sending IN packets in frame number (1 frame = 1ms) */
- /*#define VCOMPORT_IN_FRAME_INTERVAL 5
- extern uint8_t USART_Rx_Buffer[];
- extern uint32_t USART_Rx_ptr_out;
- extern uint32_t USART_Rx_length;
- extern uint8_t USB_Tx_State;*/
- u8 Receive_Buffer[64];
- /*
- *********************************************************************************************************
- * : EP1_IN_Callback
- * ÷: 1 IN°ü¨è±->PC÷
- * :
- * · :
- *********************************************************************************************************
- u8 Receive_Buffer[64];
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /*******************************************************************************
- * Function Name : EP1_OUT_Callback.
- * Description : EP1 OUT Callback Routine.
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void EP1_OUT_Callback(void)
- {
- /*u8 DataLen;
- DataLen = GetEPRxCount(ENDP1);
- PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, DataLen);
- SetEPRxValid(ENDP1);*/
- u8 DataLen;
- DataLen = GetEPRxCount(ENDP1);
- PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, DataLen);
- SetEPRxValid(ENDP1);
-
- UserToPMABufferCopy(Receive_Buffer, GetEPTxAddr(ENDP1), DataLen);
- SetEPTxCount(ENDP1, DataLen);
- SetEPTxValid(ENDP1);
-
- }
- /*
- *********************************************************************************************************
- * : EP3_OUT_Callback
- * ÷: 3 OUT°ü¨PC->è±÷
- * :
- * · :
- *********************************************************************************************************
- */
- /*void EP3_OUT_Callback(void)
- {
- uint16_t usRxCnt;
- //uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
-
- /* USB3USB_Rx_Buffer ó±USB_Rx_Cnt */
- //usRxCnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);
-
- /* */
- //usb_SaveHostDataToBuf(USB_Rx_Buffer, usRxCnt);
-
- /* í EP3 */
- //SetEPRxValid(ENDP3);
- //}
- /*
- *********************************************************************************************************
- * : SOF_Callback
- * ÷: SOF÷ .SOFhostframe·
- * :
- * · :
- *********************************************************************************************************
- */
- /*void SOF_Callback(void)
- {
- static uint32_t FrameCount = 0;
-
- if (bDeviceState == CONFIGURED)
- {
- if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
- {
- /* Reset the frame counter */
- //FrameCount = 0;
-
- /* Check the data to be sent through IN pipe */
- //EP1_IN_Callback();
- //Handle_USBAsynchXfer();
- //}
- //}
- //}
修改这个函数之后,编译时会提示重定义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) |